如何使用操作栏项启动活动

时间:2014-06-18 13:47:59

标签: android

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch(id){
    case R.id.switchActivity:
    Intent intent = new Intent(this,SecondClass.class);
    startActivity(intent);
    }

    return false;
}

当用户点击switchActivity项时,它会切换到SecondClass活动,但是当我点击switchActivity时,它总是崩溃。
Logcat:

06-18 13:51:56.909: E/AndroidRuntime(1404): FATAL EXCEPTION: main  
06-18 13:51:56.909: E/AndroidRuntime(1404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intend/com.example.intend.SecondClass}: java.lang.NullPointerException: println needs a message  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.ActivityThread.access$600(ActivityThread.java:141)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.os.Handler.dispatchMessage(Handler.java:99)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.os.Looper.loop(Looper.java:137)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.ActivityThread.main(ActivityThread.java:5041)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at java.lang.reflect.Method.invokeNative(Native Method)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at java.lang.reflect.Method.invoke(Method.java:511)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at dalvik.system.NativeStart.main(Native Method)
06-18 13:51:56.909: E/AndroidRuntime(1404): Caused by: java.lang.NullPointerException: println needs a message  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.util.Log.println_native(Native Method)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.util.Log.v(Log.java:117)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at com.example.intend.SecondClass.onCreate(SecondClass.java:28)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.Activity.performCreate(Activity.java:5104)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)  
06-18 13:51:56.909: E/AndroidRuntime(1404):     ... 11 more  

1 个答案:

答案 0 :(得分:1)

如果您尝试读取logcat日志,问题实际上不在此活动中, 它说你试图使用referance而不指定它。然后它会抛出一个NullPointerException。这就是行

at com.example.intend.SecondClass.onCreate(SecondClass.java:28)