[英]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.