Android Studio中的选项卡活动模板在Lollipop上无法使用。

3
我正在运行Android Studio 1.1.0。
我使用Android Studio向导创建了一个新项目,并选择了最小SDK:API 15和选项卡式活动。
目标SDK自动设置为API 21。
当我在Genymotion Nexus 5,API19(4.4.4)模拟器上运行此项目(未进行任何修改),一切都符合预期。
当我在Genymotion Nexus 6,API21(5.0.0)模拟器上运行此项目(未进行任何修改),我只得到一个空白屏幕。
这是MainActivity中生成的onCreate。
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

安卓工作室告诉我,
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

actionBar.setSelectedNavigationItem(position);

actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));

已经被弃用,这可能解释了为什么它们在我的API21设备上不起作用。

我该如何更改这些弃用的方法以适应API21并仍然能够正常工作?

我对Android Studio的模板无法适用于最新的API感到沮丧。


2
由于API级别21中取消了操作栏选项卡,请使用其他选项卡实现。特别是如果您想要一个 ViewPager,则在Android支持包中有 PagerTabStrip 和一些开源的选项卡(例如 PagerSlidingTabStrip)。页面为空的原因是由于 此错误 - CommonsWare
1个回答

0

简而言之,这种行为在多次运行后仍然存在,但是在删除并重新创建模拟器后现在可以正常工作了。

我实现了CommonsWare在this bug中提到的代码,但仍然看到一个空白屏幕。

我在我的代码中放置了一个断点,Genymotion Lollipop模拟器锁定了。我无法成功重新启动它。

我删除了模拟器并重新安装,现在修改后的代码正在工作。

为了验证,我像以前一样创建了一个新项目,在新创建的Lollipop模拟器中也可以工作。

令我非常沮丧的是,我无法复制我看到的原始错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接