Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

android - getActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException on new activity creation (Google - Basic Tutorial)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...