简体   繁体   中英

Intent crashing when trying to navigate to Previous activity

Basically, I don't know why, but when I try to go from one activity to the previous one ( using ym back button) the intent is null, therefore null pointer exception.

The way I do the intent is:

 Intent intent = new Intent(Puzzle_Activity.this, Play_Activity.class );
    startActivity(intent);

and there's nothing wrong with that code as far as I know. yet the logcat is giving me

 04-24 19:23:15.074: E/AndroidRuntime(829): FATAL EXCEPTION: main
04-24 19:23:15.074: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.angrywordsearch/com.example.angrywordsearch.Play_Activity}: java.lang.NullPointerException
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-24 19:23:15.074: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
04-24 19:23:15.074: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
04-24 19:23:15.074: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-24 19:23:15.074: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 19:23:15.074: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
04-24 19:23:15.074: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
04-24 19:23:15.074: E/AndroidRuntime(829):  at com.example.angrywordsearch.Play_Activity.onCreate(Play_Activity.java:60)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.Activity.performCreate(Activity.java:5008)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-24 19:23:15.074: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-24 19:23:15.074: E/AndroidRuntime(829):  ... 11 more

So what I'm asking is; Why is it null if I specify the classes to go to?

Play activity

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Intent Newintent = getParent().getIntent(); // Line 60
        Bundle extras = Newintent.getExtras();
        if(Newintent.hasExtra("userName"))
        {
            userName =  extras.getString("userName");
            Password = extras.getString("Password");
        }
            setContentView(R.layout.play_layout);       

    }

You should be using getIntent() instead of getParent().getIntent() , getParent() is probably null.

getIntent() gives you the intent that the activity was started with.

Documentation says:

public final Activity getParent () Since: API Level 1

Return the parent activity if this view is an embedded child.

Is your Activity an embedded child?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM