当我的应用以单列模式运行时,点击 ListFragment A 中的项目会使用主Activity的回调以编程方式启动片段B 以显示其他内容信息。我以这种方式启动 Fragment B :
// Create the new fragment and set an argument pointing to the entry's ID in the database
FragmentEntryDetails newFragment = new FragmentEntryDetails();
Bundle args = new Bundle();
args.putInt("entryID", entryID);
newFragment.setArguments(args);
// Specify where in the view we want to place the fragment, then add it to the backstack so the user can
// return to the previous fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
当我点击“返回”按钮返回 ListFragment A 中的搜索结果列表时,应用程序崩溃并出现以下IllegalStateException:
03-06 09:21:53.273: E/AndroidRuntime(32760): FATAL EXCEPTION: main
03-06 09:21:53.273: E/AndroidRuntime(32760): java.lang.IllegalStateException: Content view not yet created
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.ListFragment.ensureList(ListFragment.java:386)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.ListFragment.getListView(ListFragment.java:280)
03-06 09:21:53.273: E/AndroidRuntime(32760): at com.test.dictApp.FragmentSearch.onActivityCreated(FragmentSearch.java:98)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.Fragment.performActivityCreated(Fragment.java:1703)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1484)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:490)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.Activity.onBackPressed(Activity.java:2167)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.Activity.onKeyUp(Activity.java:2145)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
03-06 09:21:53.273: E/AndroidRuntime(32760): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.os.Looper.loop(Looper.java:137)
03-06 09:21:53.273: E/AndroidRuntime(32760): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-06 09:21:53.273: E/AndroidRuntime(32760): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 09:21:53.273: E/AndroidRuntime(32760): at java.lang.reflect.Method.invoke(Method.java:511)
03-06 09:21:53.273: E/AndroidRuntime(32760): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-06 09:21:53.273: E/AndroidRuntime(32760): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-06 09:21:53.273: E/AndroidRuntime(32760): at dalvik.system.NativeStart.main(Native Method)
错误指向 ListFragment A 的onActivityCreated()函数中的setAdapter():
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
// Tell the main activity that this fragment has menu elements to declare
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
adapter = new FragmentSearchCursorAdapter(getActivity(), null, 0);
// Assign our CursorAdapter
getListView().setAdapter(adapter);
Bundle args = getSQLArgs("dictionary");
// Load up the cursor that will handle the SQL grunt work
getLoaderManager().initLoader(SEARCH_LOADER_ID, args, this);
}
为什么getListView()。setAdapter()导致问题?根据Fragment生命周期,listView应该可以进行操作。如果尚未设置适配器,我应该只设置它吗?
编辑:我想出来了,请参阅下面的答案。
答案 0 :(得分:4)
我最近获得了足够的信任来回答我自己的问题,因此我决定将我的意见转化为实际答案。
解决方案是将我的FragmentSearchCursorAdapter的赋值移动到ListFragment A的onCreateView()方法,然后使用setListAdapter()而不是getListView()。setAdapter(),如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Specify the fragment layout file for this fragment class
View view = inflater.inflate(R.layout.fragment_search, container, false);
// Assign our CursorAdapter
adapter = new FragmentSearchCursorAdapter(getActivity(), null, 0);
setListAdapter(adapter);
return view;
}