我一直在关注这里的帖子,以了解有关 Android 中 ActionBar 的更多信息。
在我的应用程序中,我试图从每个页面上的操作栏图标导航到 HomeActivity .....这就是我现在处理它的方式,但它不会工作并使应用程序崩溃。
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
日志猫在这里:
03-25 20:02:26.306: E/AndroidRuntime(11346): FATAL EXCEPTION: main
03-25 20:02:26.306: E/AndroidRuntime(11346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testingapp/com.testingapp.HomeActivity}: java.lang.NullPointerException
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.os.Looper.loop(Looper.java:137)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-25 20:02:26.306: E/AndroidRuntime(11346): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:02:26.306: E/AndroidRuntime(11346): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 20:02:26.306: E/AndroidRuntime(11346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-25 20:02:26.306: E/AndroidRuntime(11346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-25 20:02:26.306: E/AndroidRuntime(11346): at dalvik.system.NativeStart.main(Native Method)
03-25 20:02:26.306: E/AndroidRuntime(11346): Caused by: java.lang.NullPointerException
03-25 20:02:26.306: E/AndroidRuntime(11346): at com.testingapp.HomeActivity.onCreate(HomeActivity.java:49)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.Activity.performCreate(Activity.java:5206)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-25 20:02:26.306: E/AndroidRuntime(11346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-25 20:02:26.306: E/AndroidRuntime(11346): ... 11 more
菜单.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"/>
</menu>