我正在关注本教程,并在此代码块的 DisplayMessageActivity 的 onCreate 方法中获取 NullPointerException:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true); //Exception here
}
我正在遵循 NexusOne AVD 模板在模拟器上运行该应用程序。
当我单击输入一些文本的发送按钮时,我得到了异常。
这是日志:
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
通过评论这个块应用程序运行成功。
为什么会抛出 NullPointerException?