Search code examples
androidandroid-toolbar

Activity creashes when i use setSupportActionBar()


I've already tried to set AppTheme windowActionBar to false, set the application's theme to my customized one and also the activity in the AndroidManifest.xml file, set the theme in the toolbar.xml but nothing works

Where's my actual code:

Toolbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.widget.Toolbar>

Styles.xml:

<style name="CustomTheme" parent = "Theme.AppCompat.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

AndroidManifest.xml :

<application>

        <activity android:name=".home.Home">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Java class:

Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_draw);
        setTitle("Coffee Hour");
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

Layout xml:

<include
    android:id="@+id/tool_bar"
    layout="@layout/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />

Solution

  • As you are using the style Theme.AppCompat.NoActionBar

    <style name="CustomTheme" parent = "Theme.AppCompat.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
    

    And You are trying to set the data on to the default Action bar using these lines

    setTitle("Coffee Hour");
    setSupportActionBar(toolbar);
    

    Remove the above lines and try to run it. It works.