尝试导航到Previous活动时意图崩溃

时间:2013-04-24 19:25:13

标签: java android android-intent

基本上,我不知道为什么,但是当我尝试从一个活动转到前一个活动时(使用ym后退按钮)  意图为null,因此空指针异常。

我的意图是:

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

据我所知,该代码没有任何问题。 但是logcat给了我

 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

所以我要问的是;如果我指定要去的类,为什么它为空?

播放活动

@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);       

    }

2 个答案:

答案 0 :(得分:4)

您应该使用getIntent()代替getParent().getIntent()getParent()可能为空。

getIntent()为您提供活动开始时的意图。

答案 1 :(得分:2)

文档说:

public final Activity getParent ()自:API级别1

如果此视图是嵌入式子级,则返回父活动。

您的活动是否是嵌入式儿童?