在ConfigChanges期间,ActionBar的导航模式发生了变化

时间:2013-08-05 06:54:01

标签: java android android-layout actionbarsherlock

我正在使用actionbarsherlock的ActionBar,第一次以横向或水平方向运行应用程序时,标签显示在Tabs Mode中。将方向更改为纵向后,导航模式仍在Tabs中。第二次将方向更改为horizontal选项卡显示为List,仅显示父项,其余项隐藏。我听说这是Android的一个错误,如果选项卡太多而无法适应屏幕,系统会自动将导航模式更改为list。但事情是我第一次加载应用程序时它甚至是水平显示为tabs模式。顺便说一下,我创建了 100个标签

此图像处于水平视图中。从顶部开始的第一张图片就是我将手机的方向更改为纵向然后返回横向后的结果。
enter image description here


我的清单文件

中包含此代码

android:configChanges="orientation|screenSize"

主要活动

 public void onConfigurationChanged(Configuration newConfig) {
         ActionBar ab = getSupportActionBar();
             ab.setDisplayShowTitleEnabled(false);

             if(ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS){
                 ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

             }
        super.onConfigurationChanged(newConfig);
}

只是为了确保我也把代码放在setContentView

附近
        final ActionBar ab = getSupportActionBar();
        ab.setDisplayShowTitleEnabled(false);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

更新:

我处理onConfigurationChanged的原因是因为我得到了空指针异常

logcat的

08-05 15:36:18.486: E/AndroidRuntime(30044): FATAL EXCEPTION: main
08-05 15:36:18.486: E/AndroidRuntime(30044): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.styled/com.actionbarsherlock.sample.styled.MainActivity}: java.lang.NullPointerException
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.access$700(ActivityThread.java:130)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.os.Looper.loop(Looper.java:137)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.main(ActivityThread.java:4745)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at java.lang.reflect.Method.invoke(Method.java:511)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at dalvik.system.NativeStart.main(Native Method)
08-05 15:36:18.486: E/AndroidRuntime(30044): Caused by: java.lang.NullPointerException
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:504)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.rotateLeftFrag(MainActivity.java:218)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.onTabSelected(MainActivity.java:256)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:356)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:356)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.app.ActionBarImpl.setNavigationMode(ActionBarImpl.java:1158)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.internal.app.ActionBarWrapper.setNavigationMode(ActionBarWrapper.java:216)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.showTabsNav(MainActivity.java:244)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.onCreate(MainActivity.java:82)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.Activity.performCreate(Activity.java:5008)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-05 15:36:18.486: E/AndroidRuntime(30044):    ... 12 more

1 个答案:

答案 0 :(得分:1)

它是android中标签的默认行为。如果水平/纵向方向的标签没有更多空间,则它将转换为列表。

现在,如果您仍希望将所有选项卡都放在列表中,那么我建议您查看此ViewPager示例,之后您必须将FragmentActivity添加到SherlockFragmentActivity中。只需查看下面的示例代码我正在做什么,这将帮助您使所有选项卡具有任何方向。

public class TestSampleTabDefault extends SherlockActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @SuppressLint("NewApi")
    public static class SampleTabsDefault extends FragmentActivity implements ServiceConnection {
        private static final String[] CONTENT = new String[] { "Albums", "Artists", "Songs", "Playlist","Genre","Recently Played","Easy Transfer" };
        Handler mHandler;
        //Runnable mRunnable;
        private ServiceToken mToken;
        private IRemoteService  mIRemoteService;
        public static ViewPager pager;
        public static int index = 0;
        public static android.app.ActionBar mActionBar;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.simple_tabs);

            //getSupportActionBar().setHomeButtonEnabled(true);
            //ActionBar bar = getSupportActionBar();
            //bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            //bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

            FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());

            pager = (ViewPager)findViewById(R.id.pager);
            pager.setAdapter(adapter);

            /*getSupportActionBar().setTitle(name);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/

            TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
            indicator.setViewPager(pager);

            index = getIntent().getExtras().getInt("index");
            if(index >0){
                indicator.setCurrentItem(index);
            }

        }

        class GoogleMusicAdapter extends FragmentPagerAdapter {
             private final FragmentManager mFragmentManager;
             private Fragment mFragmentAtPos0;
            public GoogleMusicAdapter(FragmentManager fm) {
                super(fm);
                mFragmentManager = fm;
            }
            @Override
            public Fragment getItem(int position) {
               // return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
                Fragment fm = null;

                if(position==0){// LOAD ALBUMS
                    fm = ArtistList.ArtistListFragment.newInstance();
                    return fm;

                }else if(position==1){// LOAD ARTISTS
                    //Toast.makeText(getApplicationContext(), "Select Album", Toast.LENGTH_SHORT).show();
                    fm = ArtistList.ArtistListFragment.newInstance();
                    return fm;
                }else if(position==2){// LOAD SONGS
                    fm = SongsList.SongsListFragment.newInstance(); 
                    return fm;
                }else if(position==3){   // PLaylist
                    fm = PlaylistList.PlayListFragment.newInstance();
                    return fm;
                }else if(position==4){ // Genre List
                    fm = GenreList.GenreListFragment.newInstance();
                    return fm;
                }else if(position==5){ // Recent List
                    fm = RecentAddedList.RecentListFragment.newInstance();
                    return fm;
                }else{
                    fm = TestFragment.newInstance(CONTENT[position]);
                }
                return fm;


            }

            @Override
            public CharSequence getPageTitle(int position) {
                return CONTENT[position % CONTENT.length].toUpperCase();
            }

            @Override
            public int getCount() {
              return CONTENT.length;
            }

            @Override
            public int getItemPosition(Object object)
            {
                if (object instanceof AlbumListFragment && mFragmentAtPos0 instanceof TestFragment)
                    return POSITION_NONE;
                return POSITION_UNCHANGED;
            }

        }

    }
}

希望这会对你有所帮助。祝一切顺利。如果你遇到任何困难,请告诉我。