[英]Error inflating class fragment class not found android.view.fragment but I am including the compatability library?
[英]I am getting “Error inflating class fragment”?
我有一个主要的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
Binary XML file line #14: Error inflating class fragment
检查您的Layout xml file
,在line no.14
错误。
就我而言,解决方案是写
super.onCreateView(inflater, container, savedInstanceState);
在我的onCreateView实现中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.