1

I receive the following error in Android Studio "Android Monitor"

There's a lot of mumbo-jumbo but it's essentially saying the cause of the error is the following:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual  method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a  null object reference
                                                                                 at com.example.robthoms.photopop.SettingsActivity.onCreate(SettingsActivity.java:28)

I'm trying to make Settings work on the Toolbar and I got the code from:

https://www.androidhive.info/2017/07/android-implementing-preferences-settings-screen/

If you click on this link you'll see the entire code for SettingsActivity.java that I'm trying to use but when I click on "settings" in toolbar, App closes.

What is wrong with SettingsActivity that the app closes?

My AndroidManifest.xml:

enter image description here

My styles.xml

enter image description here

Hides ActionBar in classes while styles.xml has Light.DarkActionBar

    ActionBar bar  = getSupportActionBar () ;
    if       (bar != null) bar.hide () ;
iBEK
  • 622
  • 2
  • 8
  • 27

2 Answers2

0

The problem is caused because your activity do not have any ActionBar. In android manifest, make sure your activity is using the theme which has an ActionBar. Make sure it doesn't have something like NoActionBar. If your activity is not using any theme, check your AppTheme in styles.xml.

One possible solution would be to use this in the manifest:

<activity
    android:name=".SettingsActivity"
    android:theme="@style/Theme.AppCompat.Light" />

Another way:

Add new style in styles.xml:

<style name="AppTheme.ActionBar" parent="AppTheme">
    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">false</item>
</style>

And use new style in the manifest:

<activity
    android:name=".SettingsActivity"
    android:theme="@style/AppTheme.ActionBar" />
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • I edited above and included my AndroidManifest.xml and Styles.xml, so what am I missing? What is wrong? – iBEK Oct 03 '17 at 02:31
  • In your styles.xml use Theme.AppCompat.Light instead of Theme.AppCompat.Light.NoActionBar – Nabin Bhandari Oct 03 '17 at 02:34
  • if I enable action bar in styles.xml then it'll show me an action bar across all XML files. I don't want to see action bars on any XML files but I still need the settings to work. Is there a work around? Can I make the action bar include in only specific XML files instead of all? – iBEK Oct 03 '17 at 02:53
  • You can use `Toolbar`: https://developer.android.com/training/appbar/setting-up.html – John Le Oct 03 '17 at 02:58
  • Rabin thank you but adding in this code to manifest just crashes my app again. – iBEK Oct 03 '17 at 18:00
  • @iBEK what is the new problem? – Nabin Bhandari Oct 03 '17 at 18:01
  • @iBEK post your new manifest, new styles.xml and new stacktrace of crash. – Nabin Bhandari Oct 03 '17 at 18:05
  • Now I got the settings to work but the action bar now appears on All XML files. Is there a way to hide action bar from other XML files? – iBEK Oct 03 '17 at 18:29
  • In manifest, use AppTheme for activities which don't need action bar, and use AppTheme.ActionBar ro activitiew which need action bar. – Nabin Bhandari Oct 03 '17 at 18:36
  • Actually I just figured it out and got it working with the action bar hidden in other XML files. I used the above code in various Java classes to hide action bar. – iBEK Oct 03 '17 at 18:45
0

Your style theme is noactionbar 1. If you want an actionbar use actionbar containing theme OR 2. You can also make custom view similar to actionbar