问题描述
我下面本教程并得到一个NullPointerException异常时的的onCreate方法DisplayMessageActivity在code此块:
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
}
我正在运行的Nexus One的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
通过评论此块的应用程序运行successfuly。
By commenting this block the app runs successfuly.
为什么一个NullPointerException异常抛出?
Why is a NullPointerException thrown?
推荐答案
您可能正在使用不支持主题动作条
。因此, 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(真)。引发NullPointerException异常的新活动创造(谷歌 - 基础教程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!