0

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.

paudel.sulav
  • 162
  • 1
  • 12
  • If there is no action bar then `getActionBar()` returns null... I don't understand what you are trying to do – OneCricketeer Jan 17 '16 at 05:09
  • Hope you need this http://stackoverflow.com/questions/29786011/how-to-fix-getactionbar-method-may-produce-java-lang-nullpointerexception – Viswanath Lekshmanan Jan 17 '16 at 05:09
  • @paudel.saulav are you setting any view to action bar ?? getSupportAction.setActionView( e.g toolbar> ) – dex Jan 17 '16 at 05:33
  • @cricket_007, I am trying to make navigation drawer. there I need to getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); – paudel.sulav Jan 17 '16 at 05:44

1 Answers1

0

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.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108