NappyXIII NappyXIII - 5 months ago 21
Android Question

actionbar NullPointerException with NewsReader Demo project

I tried using NewsReader to derive code for something that I'm working on and I ran into an issue with the action bar. So I decided to just recreate the NewsReader code exactly (with reasonable edits for package names and such) and I still ran into the same issue.

I hit an error with this invoke:

setUpActionBar(mIsDualPane, catIndex);


Because of this line:

actionBar.setDisplayShowTitleEnabled(false);


Full method:

public void setUpActionBar(boolean showTabs, int selTab) {
if (Build.VERSION.SDK_INT < 11) {
// No action bar for you!
// But do not despair. In this case the layout includes a bar across the
// top that looks and feels like an action bar, but is made up of regular views.
return;
}
android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
// Set up a CompatActionBarNavHandler to deliver us the Action Bar nagivation events
CompatActionBarNavHandler handler = new CompatActionBarNavHandler(this);
if (showTabs) {
actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS);
int i;
for (i = 0; i < CATEGORIES.length; i++) {
actionBar.addTab(actionBar.newTab().setText(CATEGORIES[i]).setTabListener(handler));
}
actionBar.setSelectedNavigationItem(selTab);
}
else {
actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.actionbar_list_item,
CATEGORIES);
actionBar.setListNavigationCallbacks(adap, handler);
}
// Show logo instead of icon+title.
actionBar.setDisplayUseLogoEnabled(true);
}


It seems some of the actionBar stuff is deprecated as well so that might be part of the problem. How can I fix/update this?

Stack Trace:

06-16 09:24:21.211 24376-24376/com.anothergamedesigner.newsreader E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anothergamedesigner.newsreader, PID: 24376
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anothergamedesigner.newsreader/com.anothergamedesigner.newsreader.NewsReaderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
at com.anothergamedesigner.newsreader.NewsReaderActivity.setUpActionBar(NewsReaderActivity.java:119)
at com.anothergamedesigner.newsreader.NewsReaderActivity.onCreate(NewsReaderActivity.java:81)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Answer

Change

android.app.ActionBar actionBar = getActionBar();

to

ActionBar actionBar = getSupportActionBar();

EDIT:

You also need to edit your styles.xml and your AndroidManifest.xml.

In your styles.xml, change

<style name="NewsReaderStyle" parent="android:Theme.Light.NoTitleBar">

to

<style name="NewsReaderStyle" parent="android:Theme.Light.DarkActionBar">

And in your AndroidManifest.xml, change

android:theme="@style/NewsReaderStyle_NoActionBar"

to

android:theme="@style/NewsReaderStyle"

AppCompat Styles:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

Now add android:theme="@style/AppTheme" to your manifest.