我在以下情况下收到内容视图尚未创建错误:
我在一个活动中有三个片段,片段F1,片段片段F2和片段F3。
从F1导航到F2时,F1会被添加到后台。当我从F2导航到F3时,我没有将F2添加到后台,所以当我回击时我会回到F1。 F2到F1然后F2再次按预期工作。 F1到F3然后回到F1工作正常,但如果我再次尝试从F1导航到F2,我会得到一个尚未创建的内容视图错误。
似乎错误是由getlistview引起的,但是我从创建的onactivity调用getlistview,这应该在创建视图后调用,因此我不确定错误的来源。< / p>
感谢任何人提供的任何帮助!
附加编辑代码
logcat的:
08-19 01:11:13.787:E / AndroidRuntime(1289):致命异常:主要 08-19 01:11:13.787:E / AndroidRuntime(1289):java.lang.IllegalStateException:尚未创建的内容视图 08-19 01:11:13.787:E / AndroidRuntime(1289):在android.support.v4.app.ListFragment.ensureList(ListFragment.java:328) 08-19 01:11:13.787:E / AndroidRuntime(1289):在android.support.v4.app.ListFragment.getListView(ListFragment.java:222) 08-19 01:11:13.787:E / AndroidRuntime(1289):at com.alex.hours.RestaurantListFragment $ 6.done(RestaurantListFragment.java:418) 08-19 01:11:13.787:E / AndroidRuntime(1289):at com.parse.FindCallback.internalDone(FindCallback.java:45) 08-19 01:11:13.787:E / AndroidRuntime(1289):at com.parse.FindCallback.internalDone(FindCallback.java:31) 08-19 01:11:13.787:E / AndroidRuntime(1289):at com.parse.Parse $ 5 $ 1.run(Parse.java:891) 08-19 01:11:13.787:E / AndroidRuntime(1289):在android.os.Handler.handleCallback(Handler.java:730) 08-19 01:11:13.787:E / AndroidRuntime(1289):在android.os.Handler.dispatchMessage(Handler.java:92) 08-19 01:11:13.787:E / AndroidRuntime(1289):在android.os.Looper.loop(Looper.java:137) 08-19 01:11:13.787:E / AndroidRuntime(1289):在android.app.ActivityThread.main(ActivityThread.java:5103) 08-19 01:11:13.787:E / AndroidRuntime(1289):at java.lang.reflect.Method.invokeNative(Native Method) 08-19 01:11:13.787:E / AndroidRuntime(1289):at java.lang.reflect.Method.invoke(Method.java:525) 08-19 01:11:13.787:E / AndroidRuntime(1289):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 08-19 01:11:13.787:E / AndroidRuntime(1289):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-19 01:11:13.787:E / AndroidRuntime(1289):at dalvik.system.NativeStart.main(Native Method)
相关代码:
从导航栏中创建F1片段:
private void selectItem(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager;
Bundle args = new Bundle();
Log.i("POSITION", "position: " + position);
switch (position) {
case 0:
MainActivityFragment mainActivity = new MainActivityFragment();
fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, mainActivity)
.commit();
break;
case 1:
args.putString(RestaurantListFragment.QUERY_CODE,
RestaurantListFragment.ALL_RESTAURATNS);
RestaurantListFragment myRestaurants = new RestaurantListFragment();
myRestaurants.setArguments(args);
fragmentManager = getSupportFragmentManager();
if(fragmentManager.getBackStackEntryCount()>0){
fragmentManager.popBackStack();
}
fragmentManager.beginTransaction()
.replace(R.id.content_frame, myRestaurants)
.addToBackStack(null).commit();
break;
}
创建F2片段:
args.putString(RestaurantListFragment.QUERY_CODE, RestaurantListFragment.ALL_RESTAURATNS);
RestaurantListFragment allRestaurants = new RestaurantListFragment();
allRestaurants.setArguments(args);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, allRestaurants)
.addToBackStack(null).commit();
通过listview中的点击创建F3
public void onListItemClick(ListView l, View v, int position, long id) {
Restaurant r = ((RestaurantAdapter) getListAdapter()).getItem(position);
Bundle args = new Bundle();
if (getArguments() != null) {
String queryCode = getArguments().getString(QUERY_CODE);
args.putString(QUERY_CODE, queryCode);
}
args.putString(RestaurantFragment.EXTRA_RESTAURANT_ID, r.getObjectId());
RestaurantFragment restaurantFragment = new RestaurantFragment();
restaurantFragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, restaurantFragment)
.addToBackStack(null).commit();
抛出错误的代码:
if (getListView().getAdapter() == null) {
RestaurantAdapter adapter = new RestaurantAdapter(
getListView().getContext(),
mRestaurants);
setListAdapter(adapter);