Skip to content Skip to sidebar Skip to footer

Nullpointerexception: With Actionbar.setdisplayhomeasupenabled(boolean)' On A Null Object Reference

I get this nullPointerException on runtime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boole

Solution 1:

The cause of your issue is using MainActivity extend Activity with support theme style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar". It's incompatible things. Which min sdk you need?

In your code having MainActivity extends Activity you don't need AppCompatTheme. Use name="AppTheme" parent="android:Theme.Light"

If you are using Theme.AppCompat.Light.DarkActionBar, you should extend your Activity from AppCompatActivity, and use getSupportActionBar().

Instead of:

publicclassMainActivityextendsActivity {

use:

publicclassMainActivityextendsAppCompatActivity {

and instead of:

getActionBar().setTitle(mTitles);

use:

getSupportActionBar().setTitle(mTitles);

Solution 2:

This problem might be caused by your theme. Check it again, and make sure that it parent with Theme.AppCompat.Light.DarkActionBar.

<stylename="MyTheme"parent="Theme.AppCompat.Light.DarkActionBar"><itemname="android:windowNoTitle">true</item><itemname="windowActionBar">true</item>
    ...
</style>

If your activity extends AppCompatActivity or ActionBarActivity, call getSupportActionBar().

Solution 3:

Put assert getActionBar () != null; after mActionBar = getActionBar();

Solution 4:

In my case, I forgot to init my Toolbar, so, before using getSupportActionBar, I had to do this:

    appbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(appbar);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_menu);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Solution 5:

If you check this answer in 2019 like me , the problem is about your android manifest:

<applicationandroid:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

Check the documentation here:

https://developer.android.com/training/appbar/setting-up.html

You also take in mind the previous answer from TetianaDev:

Instead of:

publicclassMainActivityextendsActivity {

use:

publicclassMainActivityextendsAppCompatActivity {

and instead of:

getActionBar().setTitle(mTitles);

use:

getSupportActionBar().setTitle(mTitles);

This works for me.

Post a Comment for "Nullpointerexception: With Actionbar.setdisplayhomeasupenabled(boolean)' On A Null Object Reference"