Search code examples
androidandroid-actionbar

I already set the actionbar but getSupportActionBar() returns null


If I go to another activity, it will crash and say my action bar is null I already set it but why it's still be null? Can someone help me?

My manifest code:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".Main"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity
        android:name=".DaftarMurid"
        android:label="Daftar Murid" />
    <activity
        android:name=".Settings"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".Main">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="arvin.a8c_smpbruder.Main" />
    </activity>
    <activity
        android:name=".JadwalPelajaran"
        android:label="Jadwal Pelajaran" />
    <activity
        android:name=".UploadFoto"
        android:label="Upload Foto" />
    <activity
        android:name=".GaleriFoto"
        android:label="Galeri" />
    <activity
        android:name=".About"
        android:label="Tentang" />
    <activity
        android:name=".Contributors"
        android:label="Kontributor" />
    <activity
        android:name=".LoadingHome"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TampilanAkun"
        android:label="Akun" />
    <activity android:name=".JadwalPiket"></activity>
</application>

My Main Activity Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //signIn.setOnClickListener(this);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

I set the actionbar on there, but still null. Can someone help me ?

My main activity layout xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

My toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="arvin.a8c_smpbruder.Main">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Solution

  • Check your Styles.xml if there the current style support the ActionBar. Or check the activity your going to start support the ActionBar and try removing the label tag in your second activity.