2013-12-20 67 views
0

這是一個具有SlidingMenu和PagerSlidingTabStrip的應用程序,主要活動包含一個framelayout,當我點擊列表項時,slidingmenu應該替換容器中的碎片。這些片段通過FragmentStatePagerAdapter存放其他片段。旋轉致命異常

應用程序啓動正常,第一個片段加載,當我點擊菜單上的列表項時,其他片段也加載正常,但是當我旋轉時,我得到一個致命的異常。但是,當我第一次加載應用程序並旋轉時,第一個片段在旋轉時很好。所以它必須是我如何替換我的onItemClick中的片段。任何人都可以給我任何指針?

MainActivity.java

public class MainActivity extends SlidingFragmentActivity { 


@TargetApi(11) 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 


    getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.mainContentFrame, new WorkOutsContainerFrag()) 
    .commit(); 

    setBehindContentView(R.layout.menu); 

    ActionBar actionBar = getActionBar(); 

     populate(); 

     SlidingMenu menu = getSlidingMenu(); 

     menu.setMode(SlidingMenu.LEFT); 
     menu.setShadowWidthRes(R.dimen.shadow_width); 
     menu.setShadowDrawable(R.drawable.shadow); 
     menu.setBehindWidthRes(R.dimen.slidingmenu_offset); 
     menu.setFadeDegree(0.35f); 
     menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 
     menu.setSlidingEnabled(false); 
     setSlidingActionBarEnabled(true); 

     actionBar.setDisplayHomeAsUpEnabled(true); 



    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      toggle(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    private void populate() { 

     ListView lv = (ListView) findViewById(R.id.listView1); 
     String[] values = new String[] { "Workouts", "Information", "Ladders"}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, android.R.id.text1, values); 
     lv.setAdapter(adapter); 


     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       SlidingMenu menu = getSlidingMenu(); 
       WorkOutsContainerFrag workoutsContainerFrag = new WorkOutsContainerFrag(); 
       InformationContainerFrag informationContainerFrag = new InformationContainerFrag(); 
       LaddersContainer laddersContainer = new LaddersContainer(); 

       switch (position) { 
       case 0: 
        getSupportFragmentManager().beginTransaction() 
        .addToBackStack(null) 
        .replace(R.id.mainContentFrame, workoutsContainerFrag) 
        .commit(); 
        menu.toggle(); 
        break; 

       case 1: 
        getSupportFragmentManager().beginTransaction() 
        .addToBackStack(null) 
        .replace(R.id.mainContentFrame, informationContainerFrag) 
        .commit(); 
        menu.toggle(); 
        break; 

       case 2: 
        getSupportFragmentManager().beginTransaction() 
        .addToBackStack(null) 
        .replace(R.id.mainContentFrame, laddersContainer) 
        .commit(); 
        menu.toggle(); 
        break; 
       } 
      } 
     }); 





    } 

}

logcat的

12-20 17:05:42.354: E/AndroidRuntime(2977): FATAL EXCEPTION: main 
12-20 17:05:42.354: E/AndroidRuntime(2977): java.lang.RuntimeException: Unable to  start activity ComponentInfo{com.racepace.onewest/com.racepace.onewest.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f040056 (com.racepace.onewest:id/workoutspager) for fragment GymWorkoutsFragment{418b1e20 #1 id=0x7f040056} 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread.access$700(ActivityThread.java:141) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.os.Looper.loop(Looper.java:137) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at java.lang.reflect.Method.invoke(Method.java:525) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at dalvik.system.NativeStart.main(Native Method) 
12-20 17:05:42.354: E/AndroidRuntime(2977): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f040056 (com.racepace.onewest:id/workoutspager) for fragment GymWorkoutsFragment{418b1e20 #1 id=0x7f040056} 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1084) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1875) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.Activity.performStart(Activity.java:5143) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 
12-20 17:05:42.354: E/AndroidRuntime(2977):  ... 12 more 

回答

0

嘗試在所有的片段,呼籲Fragment.setRetainInstanceState(真)你實例化它們像這樣後:

WorkOutsContainerFrag workoutsContainerFrag = new WorkOutsContainerFrag(); 
workoutsContainerFrag.setRetainInstanceState(true); 

另一件事,你不需要做onCreate()中的片段事務,因爲你直接調用populate()並在它顯示在屏幕上之前用不同的片段替換它。