我得到“错误膨胀类片段”?

时间:2012-08-07 12:01:12

标签: android android-fragments android-listfragment

我有一个主Activity,它有两个片段,ListFramgment和DetailFragment。 在ListFragment中,如果选择了一个选项,我正在查询将返回数组的数据库。我将此数组传递给DetailFragment。

public void onListItemClick(ListView l, View v, int position, long id) {
    String selectedStore = (String) getListAdapter().getItem(position);
    DetailFragment fragment = (DetailFragment) getFragmentManager()
            .findFragmentById(R.id.detailFragment);
    if (fragment != null && fragment.isInLayout()) {
        TestAdapter mDbHelper = new TestAdapter(getActivity());
        mDbHelper.createDatabase();
        mDbHelper.open();

        Cursor curItemList = mDbHelper.getItemList(selectedStore);
        String[] itemList = new String[curItemList.getCount()];
        int i = 0;
        while(curItemList.moveToNext()){
            String item = curItemList.getString(0);
            itemList[i] = item;
            i++;
        }


        mDbHelper.close();

        fragment.setText(itemList);

        //getItemList(selectedStore);



    }

这是DetailFragment代码:

    public class DetailFragment extends android.app.ListFragment {

//   String qty;
//   String pr;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.details, container, false);
        return view;
    }

    public void setText(final String[] itemList) {
        ListView view = (ListView) getView().findViewById(R.id.detailsText);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, itemList);
        view.setAdapter(adapter);
}

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Toast.makeText(null, "time for popup", Toast.LENGTH_SHORT).show();
    }

当应用程序启动时,它崩溃并且LogCat为:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.click4tab.fragmentvogella/com.click4tab.fragmentvogella.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment

2 个答案:

答案 0 :(得分:1)

Binary XML file line #14: Error inflating class fragment

检查 Layout xml file line no.14 时出现错误。

答案 1 :(得分:0)

就我而言,解决方案是写作

super.onCreateView(inflater, container, savedInstanceState);

在我的onCreateView实现中。