setDisplayHomeAsUpEnabled

setDisplayHomeAsUpEnabled

本文介绍了getActionBar().setDisplayHomeAsUpEnabled(true);在创建新活动时抛出 NullPointerException(Google - 基础教程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 本教程 并在这段代码中DisplayMessageActivity的onCreate方法:

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
}

我正在模拟器上按照 NexusOne AVD 模板运行该应用程序.
当我单击输入了一些文本的发送按钮时,出现异常.

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.

这是日志:

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.

为什么会抛出 NullPointerException?

Why is a NullPointerException thrown?

推荐答案

您可能正在使用不支持 ActionBar 的主题.因此 getActionBar() 方法抛出 NullPointerException.

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

尝试使用此主题:

android:theme="@android:style/Theme.Holo.Light"

这篇关于getActionBar().setDisplayHomeAsUpEnabled(true);在创建新活动时抛出 NullPointerException(Google - 基础教程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:55