Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This Activity already has an action bar supplied by the window decor (FEATURE_ACTION_BAR)

Tags:

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

This is the error I got, I searched for solutions but I didn't find how to solve it. This is my code:

styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/myPrimaryColor</item>
    <item name="colorPrimaryDark">@color/myPrimaryDarkColor</item>
    <item name="colorAccent">@color/myAccentColor</item>

    <item name="android:textColorPrimary">@color/myTextPrimaryColor</item>
</style>

<style name="ActionBarPopupThemeOverlay" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

<style name="Toolbartitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="ToolbarSubtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textColor">#56FFFFFF</item>
</style>

send_comment.xml(my activity layout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/my_toolbar"
            android:layout_height="128dp"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"
            android:layout_width="match_parent"
            android:background="?attr/colorPrimary"
            android:paddingLeft="72dp"
            android:paddingBottom="16dp"
            android:gravity="bottom"
            app:titleTextAppearance="@style/Toolbartitle"
            app:subtitleTextAppearance="@style/ToolbarSubtitle"
            app:theme="@style/ThemeOverlay.AppCompat.Light"
            android:title="תגובה"
            />
</RelativeLayout>

SendComment.java

public class SendComment extends ActionBarActivity {
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_comment);
        toolbar = (Toolbar) findViewById(R.id.my_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }
}

What am I doing wrong?

like image 426
Haim127 Avatar asked Jan 05 '15 17:01

Haim127


People also ask

Where the action bar is present in the activity screen by default?

Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default.

What is windowActionBar in Android?

android:windowActionBar denotes property for lollipop and above only. Where as windowActionBar denotes for all versions and is retrieved from support library. Follow this answer to receive notifications.

How do I add action to action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

How do I get the toolbar from action bar?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar.


2 Answers

In your AppTheme style use,

Theme.AppCompat.Light.NoActionBar

instead of

Theme.AppCompat.Light.DarkActionBar

You are setting a theme that has an actionbar and then you are setting toolbar as an actionbar. That's why your are getting this error.

Use the theme that has no actionbar instead and it will fix the issue.

like image 119
Hirak Chhatbar Avatar answered Oct 05 '22 11:10

Hirak Chhatbar


Use style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" and everywhere use the same Theme as this maintain flow of Context to Android as it do not get confused between two ActionBar.

like image 29
Ashwani Avatar answered Oct 05 '22 11:10

Ashwani