安卓工作室中的Tabbed活动模板不适用于棒棒糖



我正在运行Android Studio 1.1.0。

我使用Android Studio向导创建了一个新项目,并选择了MinimumSDK:API 15和Tabbed Activity。

目标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上工作,并且仍然在

安卓工作室的模板不适用于最新的API,这让我很沮丧。

Tl;dr这种行为在多次运行中持续存在,但在删除并重新创建模拟器后仍然有效

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

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

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

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

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

最新更新