为什么android坚持认为我已经有动作吧?

时间:2015-10-21 08:50:28

标签: android android-actionbar appcompatactivity

按照这里的教程:http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html#尝试执行材料设计滑动标签我按照指示完成了所有操作。

但是,当我运行应用程序时,它给了我以下消息:

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我试图解决这个问题但尝试了许多事情,但没有成功。

最后我来到了这个问题stackoverflow.com/questions/29790070/upgraded-to-appcompat-v22-1-0-and-now-getting-illegalargumentexception-appcompa并接受了回答的建议。

我更改了styles.xml,如下所示:

<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

</style>

尽管如此,它仍然给出了同样的错误信息。

为了完成,这是我的活动:

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private HomeTabsPagerAdapter mPageAdapter;

    private Toolbar mToolbar;
    private SlidingTabLayout mTabs;


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

                setContentView(R.layout.activity_main);

                setViewsClassMembers();

                setSupportActionBar(mToolbar);
                mPageAdapter = new HomeTabsPagerAdapter(getSupportFragmentManager());

                mViewPager.setAdapter(mPageAdapter);
                // Assiging the Sliding Tab Layout View
                mTabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

                // Setting Custom Color for the Scroll bar indicator of the Tab View
                mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
                    @Override
                    public int getIndicatorColor(int position) {
                        return getResources().getColor(R.color.tabsScrollColor);
                    }
                });

                // Setting the ViewPager For the SlidingTabsLayout
                mTabs.setViewPager(mViewPager);

    }


    private void setViewsClassMembers() {
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mToolbar = (Toolbar) findViewById(R.id.tool_bar);
        mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
        case R.id.action_new_request:
            startNewRequestActivity();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void startNewRequestActivity() {
        Intent intent = new Intent(this, NewRequestActivity.class);
        startActivity(intent);
    }

}

任何人都可以看到问题出在哪里?为什么Android坚持说我已经有了行动吧?

1 个答案:

答案 0 :(得分:0)

请参阅:https://stackoverflow.com/a/26515159

<item name="windowActionBar">false</item> 

在styles.xml中。