Search code examples
androidnavigation-drawerandroid-theme

In Navigation Drawer, getActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException


I am trying to make the Navigation drawer in my welcome page after login. But

getActionBar().setDisplayHomeAsUpEnabled(true);

throws Null pointer exception. In logcat, I got the message:

Caused by: java.lang.NullPointerException
at com.samvardhan.org.vaccinationinfo.ProfileActivity.onCreate(ProfileActivity.java:170)

I am using

Theme.AppCompat.NoActionBar . I tried to change the theme to

android:theme="@android:style/Theme.Holo.Light"

This also didn't worked. I want to use

Theme.AppCompat.NoActionBar

How to solve this issue. This

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

should not give me null value. This getActionBar() is giving me null. How could I solve this one with same theme.


Solution

  • Use:

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    

    in your Styles and make sure you've added The Toolbar in your XML:

    Example:

    <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"/>
    

    And of course, in your OnCreate:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    

    And use this for AppCompat:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    

    That should solve your problem.