21

I am following this tutorial and getting a NullPointerException at the onCreate method of the DisplayMessageActivity at this block of code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true); //Exception here
}

I am running the app at an emulator following the NexusOne AVD template.
When I click the send button with some text typed in I get the exception.

Here is the log:

09-03 23:02:07.586: E/AndroidRuntime(7095): FATAL EXCEPTION: main
09-03 23:02:07.586: E/AndroidRuntime(7095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage.myfirstapp/com.mypackage.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.os.Looper.loop(Looper.java:137)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at java.lang.reflect.Method.invoke(Method.java:511)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at dalvik.system.NativeStart.main(Native Method)
09-03 23:02:07.586: E/AndroidRuntime(7095): Caused by: java.lang.NullPointerException
09-03 23:02:07.586: E/AndroidRuntime(7095):     at com.mypackage.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:40)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.Activity.performCreate(Activity.java:5104)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-03 23:02:07.586: E/AndroidRuntime(7095):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-03 23:02:07.586: E/AndroidRuntime(7095):     ... 11 more

By commenting this block the app runs successfuly.

Why is a NullPointerException thrown?

1
  • 9
    This means that action bar does not exist. See if your theme mentioned in manifest file supports action bar. Commented Sep 3, 2013 at 23:31

7 Answers 7

32

You are probably using a Theme that doesn't support ActionBar. Hence getActionBar() method throws NullPointerException.

Trying using this theme:

android:theme="@android:style/Theme.Holo.Light"
1
  • You probably have a ThemeName.NoActionBar in your AndroidManifest.xml... Just remove the .NoActionBar part and you're good to go! Check res/styles.xml for available styles... (Android Studio here, in other editors you have to code the style yourself)
    – varun
    Commented Mar 1, 2019 at 14:44
31

Try changing getActionBar() to getSupportActionBar() or ((ActionBarActivity)getActivity()).getSupportActionBar().

3
  • 11
    Any explanation on why this solves the problem would improve your answer.
    – ryanyuyu
    Commented Feb 20, 2015 at 16:45
  • I also had this problem recently, and solved it by doing this.
    – ewige
    Commented Feb 21, 2015 at 10:05
  • 1
    Change import android.app.ActionBar; to import android.support.v7.app.ActionBarActivity; Commented Jun 11, 2015 at 7:24
11

I had the same problem.

In the activity of the manifest i had declared

android:theme="@android:style/Theme.Black.NoTitleBar

which caused the error. After removing this line, my actionbar worked fine.

4

It shouldn't be necessary but there are some behavior inconsistencies between API versions even after API level 14.

Behavior:

Back "<" image is displayed but it doesn't work when pressed. As a good practice I use to implement the switch case in order to handle back event.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

It worked for me.

1

I encountered this error when I was trying out the navigation drawer sample app available at the android developer site. It turns out the MainActivity did not extend the ActionBarActivity and thus getActionBar() returns null causing the exception.

0

None of the other answers worked for me, really. I just commented out that whole if and it worked. From the method name (and documentation), you don't need that functionality anyway (unless you want it), so no harm done.

Relevant documentation excerpt:

Set whether home should be displayed as an "up" affordance. Set this to true if selecting "home" returns up by a single level in your UI rather than back to the top level or front page.

To set several display options at once, see the setDisplayOptions methods.

Parameters

showHomeAsUp true to show the user that selecting home will return one level up rather than to the top level of the app.

0

You must have set some theme with your Activity which is not compatible with the Action bar.

So just check the theme you are using with in your manifest file and remove it

or if you have defined custom theme go to your res->values->style.xml and make WindowActionBar to true.

Not the answer you're looking for? Browse other questions tagged or ask your own question.