user2477
user2477

Reputation: 997

How to startActivity using actionbar item

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;
}

When users click on switchActivity item, it will switch to SecondClass activity, but when i click on switchActivity, it always crashes.
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  

Upvotes: 0

Views: 329

Answers (1)

Sercan Ozdemir
Sercan Ozdemir

Reputation: 4692

The problem is not in this activity actually if you try to read your logcat logs, it says you are trying to use a referance without assigning it. And then it throws a NullPointerException. And here's the line

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

Upvotes: 1

Related Questions