景观到肖像例外

时间:2014-07-07 11:09:55

标签: android android-fragments tabs

我已经创建了一个Tab栏,我已经锁定了横向模式,并且工作正常,但是我已经修改了纵向/横向模式的程序,并且在旋转手机时我收到了异常。 这是MainActivity中的代码。

protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "MainActivity.onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = this.getBaseContext();
        jsonIng = new String();
        jsonRct = new String();
//      progressBarHandler = new Handler(); 
        rct = new Ricetta();


        final ActionBar actionBar = getActionBar();

//          actionBar.setHomeButtonEnabled(false);
//          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//          actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

            mViewPager = new ViewPager(this);
            mViewPager = (ViewPager) findViewById(R.id.pager);
            setContentView(mViewPager);

            mTabsAdapter = new TabsAdapter(this, mViewPager);
            mTabsAdapter.addTab(actionBar.newTab()
                        .setText(R.string.title_section1)
                        .setIcon(R.drawable.ic_menu_agenda),
                        Page1.class, null);

            mTabsAdapter.addTab(actionBar.newTab()
                        .setText(R.string.title_section2)
                        .setIcon(R.drawable.ic_menu_show_list),
                        Page2.class, null);

            mTabsAdapter.addTab(actionBar.newTab()
                        .setText(R.string.title_section3)
                        .setIcon(R.drawable.ic_menu_show_list),
                        Page3.class, null);

            mTabsAdapter.addTab(actionBar.newTab()
                        .setText(R.string.title_section4)
                        .setIcon(R.drawable.compare48x48),
                        Page4.class, null); 

            mTabsAdapter.addTab(actionBar.newTab()
                    .setText(R.string.title_section5)
                    .setIcon(R.drawable.ic_menu_allfriends),
                    Page5.class, null);      

            if (savedInstanceState != null) {
                actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
            }   

            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 

... ...

这是LOG:

07-04 19:25:19.693: I/Pagina1(1717): On Pause .....
07-04 19:25:19.693: I/Pagina4(1717): On Pause .....
07-04 19:25:19.697: I/Pagina1(1717): On Stop .....
07-04 19:25:19.697: I/Pagina4(1717): On Stop .....
07-04 19:25:19.697: I/Pagina1(1717): On Destroy .....
07-04 19:25:19.697: I/Pagina4(1717): On Destroy .....
07-04 19:25:19.749: D/MainActivity(1717): MainActivity.onCreate
07-04 19:25:19.869: D/dalvikvm(1717): GC_FOR_ALLOC freed 4444K, 25% free 14284K/18896K, paused 34ms, total 35ms
07-04 19:25:19.881: D/AndroidRuntime(1717): Shutting down VM
07-04 19:25:19.881: W/dalvikvm(1717): threadid=1: thread exiting with uncaught exception (group=0xa4c23648)
07-04 19:25:19.885: E/AndroidRuntime(1717): FATAL EXCEPTION: main
07-04 19:25:19.885: E/AndroidRuntime(1717): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bandweb.myrecipescreator/com.bandweb.myrecipescreator.MainActivity}: java.lang.IllegalStateException: setSelectedNavigationIndex not valid for current navigation mode
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.os.Looper.loop(Looper.java:137)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at java.lang.reflect.Method.invoke(Method.java:525)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at dalvik.system.NativeStart.main(Native Method)
07-04 19:25:19.885: E/AndroidRuntime(1717): Caused by: java.lang.IllegalStateException: setSelectedNavigationIndex not valid for current navigation mode
07-04 19:25:19.885: E/AndroidRuntime(1717):     at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:364)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at com.bandweb.myrecipescreator.MainActivity.onCreate(MainActivity.java:390)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.Activity.performCreate(Activity.java:5133)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-04 19:25:19.885: E/AndroidRuntime(1717):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

编辑更新

public static class TabsAdapter extends FragmentPagerAdapter
        implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(FragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
            mActionBar.setSelectedNavigationItem(position);
            //Log.i(TAG, "onPageSelected " + ", position " + Integer.toString(position));   
            //Page2.listAdapter.notifyDataSetChanged();

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }



    }

1 个答案:

答案 0 :(得分:-1)

Caused by: java.lang.IllegalStateException: setSelectedNavigationIndex not valid for current navigation mode

这是你的问题

尝试保存当前的选择标签ID并在旋转后重新创建

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
    // onCreate implementation goes here
    if (savedInstanceState != null) {
        int selectedTabIndex = savedInstanceState.getInt("selectedTabIndex");
        getActionBar().setSelectedNavigationItem(selectedTabIndex);
    }
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("selectedTabIndex", getActionBar().getSelectedNavigationIndex());
}