2013-07-14 91 views
3

我正在使用嵌套片段並且片段加載得很好。但是當我在此frameLayout中加載另一個片段並且想要再次加載嵌套片段時,我的應用程序崩潰。當我第二次導航到嵌套片段時,應用程序崩潰

07-14 16:58:21.272: E/AndroidRuntime(1813): java.lang.IllegalStateException: Activity has been destroyed 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at me.kaidul.uhunt.Fragment1.onCreateView(Fragment1.java:23) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
07-14 16:58:21.272: E/AndroidRuntime(1813):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 

我父片段類:

public class Fragment1 extends SherlockFragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment1, container, false); 
     Fragment fragmentC = new HeadlinesFragment(); 
     Fragment frag = new Child(); 
     FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
     transaction.add(R.id.child_fragment, fragmentC); 
     transaction.add(R.id.child_fragment2, frag); 
     transaction.commit(); 
     return rootView; 
    } 

} 

編輯: 這是我所有的片段添加到活動:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     // Locate Position 
     switch (position) { 
     case 0: 
      ft.replace(R.id.content_frame, fragment1); 
      break; 
     case 1: 
      ft.replace(R.id.content_frame, fragment2); 
      break; 
     case 2: 
      ft.replace(R.id.content_frame, fragment3); 
      break; 
     } 
     ft.commit(); 
+0

如何將Fragment1和其他添加到您的活動? –

+0

@MaciejGórski我編輯了我的問題並添加了片段以將片段添加到活動中。請看看 –

+1

可能與本答案中引用的錯誤有關 - http://stackoverflow.com/a/15656428/574785 – ErikR

回答

相關問題