2013-03-06 51 views
2

當我的應用程序以單列模式運行時,點擊中的項目ListFragment A使用回調函數以編程方式啓動片段B以顯示其他信息。我啓動片段B以這種方式:片段的onActivityCreated中的setAdapter()在點擊Back按鈕時導致「IllegalStateException:內容視圖尚未創建」

​​

當我打的後退按鈕返回到搜索結果列表中ListFragment一個,應用程序崩潰與以下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甲的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()會導致問題?根據片段生命週期,listView應該可用於操作。我是否應該只在適配器尚未設置的情況下進行設置?

編輯:我想通了,見下面的答案。

回答

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; 
} 
相關問題