二进制XML文件第11行:膨胀类片段时出错

4

我的代码出现了两个错误。

由此引起的错误: java.lang.IllegalStateException: Fragment com.example.dfoley.write_to_file.topFragment 没有创建视图。

由此引起的错误: android.view.InflateException: Binary XML file line #11: Error inflating class fragment,这两个错误都指向 MainActivity.java 的第21行,即以下内容:setContentView(R.layout.activity_main);

bottomFragment

package com.example.dfoley.write_to_file;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class bottomFragment extends ListFragment {
    private ArrayAdapter<StateUser> adapter;

    @Override
    public void onActivityCreated(Bundle saveInstanceState){
        ArrayList<StateUser> flight = MainContoller.getInstance().getFlights();
        this.adapter = new ArrayAdapter<StateUser>(getActivity(), android.R.layout.simple_list_item_1, flight);
        setListAdapter(this.adapter);
        super.onActivityCreated(saveInstanceState);
    }
    public void refreshList(){
        this.adapter.notifyDataSetChanged();
    }
}

顶部碎片

package com.example.dfoley.write_to_file;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import.android.util.Log;
import android.view.View;
import.android.widget.Button;
import android.widget.EditText;

import java.io.IOException; 
import java.io.OutputStreamWriter;



public class topFragment extends Fragment{
    private FlightSearcher searcher;
    EditText text1;

    public interface FlightSearcher {
        public void refreshFlightList();
    }

    @Override
    public void onAttach(Activity activity) {
        searcher = (FlightSearcher) activity;
        super.onAttach(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        setupListeners();
        super.onActivityCreated(savedInstanceState);
    }

    public void setupListeners() {
        Button addUser = (Button)getActivity().findViewById(R.id.button);
        addUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeToFile();
                searcher.refreshFlightList();
            }
        });
    }

    private void writeToFile() {
        text1=(EditText)getActivity().findViewById(R.id.editText);
        String AddUsers = text1.getText().toString();
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(getActivity().openFileOutput("UserList", Context.MODE_PRIVATE));
            outputStreamWriter.write(AddUsers);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }
}

主活动

package com.example.dfoley.write_to_file;

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements topFragment.FlightSearcher{

    public void refreshFlightList() {
        FragmentManager mgr = getFragmentManager();
        bottomFragment bottomFragmentRef =(bottomFragment) mgr.findFragmentById(R.id.bottom_fragment);
        bottomFragmentRef.refreshList();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activiy_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.dfoley.write_to_file.topFragment"
    android:id="@+id/top_fragment"
    android:layout_weight="1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:layout="@layout/topfragment" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.dfoley.write_to_file.bottomFragment"
    android:id="@+id/bottom_fragment"
    android:layout_weight="1"
    android:layout_below="@+id/top_fragment"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:layout="@layout/bottomfragment" />

2个回答

4

将片段更改为FrameLayout

    <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

3
对于你的两个片段,你没有告诉它如何创建视图。我看到你正在使用tools:layout标签,但根据Tools文档,那只是对设计师的提示;它实际上并没有填充该布局:
“此属性通常在中设置,并用于记录您要在设计时看到呈现的布局(在运行时,这将由标记列出的片段类的操作确定)。”
因此,你需要重写onCreateView方法,填充你的视图层次结构,然后返回它:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.topfragment, container, false);
}

你好,我们应该在哪里添加这个方法呢?我没有一个继承Fragment的类,只有一个FragmentActivity,但是我仍然遇到了相同的错误。 - JollyRoger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接