问题
Following the tutorial here: http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html# trying to perform material design sliding tabs I did everything as instructed.
However, when I run the app it gave me the following message:
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.
I tried to resolve this and tried many thing, with no success.
Eventually I came to this SO question stackoverflow.com/questions/29790070/upgraded-to-appcompat-v22-1-0-and-now-getting-illegalargumentexception-appcompa and took the answered suggestion.
I changed my styles.xml as follows:
<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
Nevertheless, it still gives me the same error message.
Just for the sake of completion, here is my Activity:
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);
}
}
Can anyone see where is the problem? why do Android insists that I am already having action bar?
回答1:
See it: https://stackoverflow.com/a/26515159
<item name="windowActionBar">false</item>
inside your styles.xml.
来源:https://stackoverflow.com/questions/33254840/why-do-android-insists-that-i-already-have-action-bar