轮换时的致命异常

时间:2013-12-20 22:36:47

标签: android android-fragments

这是一个拥有SlidingMenu和PagerSlidingTabStrip的应用程序,主要活动包含一个framelayout,当我点击列表项时,滑动菜单应该替换容器中的碎片。这些片段通过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

1 个答案:

答案 0 :(得分:0)

尝试实例化它们后,尝试在所有片段上调用Fragment.setRetainInstanceState(true):

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

另一件事,你不需要在onCreate()中进行片段事务,因为你立即调用populate()并在它之前用一个不同的片段替换它。显示在屏幕上。