我有一项任务。我必须将游戏的要点从GameActivity放到GameOverActivity。我尝试了几个小时,但没有用...这是我的尝试:

这是在我的GameActivity中(额外输入):

public void finishGameActivity(){
    Intent i = getIntent();
    i.putExtra("key1", points);
    setResult(Activity.RESULT_OK, i);
    finish();
}

这是我的GameOverActivity:
    Bundle bundle = getIntent().getExtras();
    int value = bundle.getInt("key1");
    TextView points_stats = (TextView) findViewById(R.id.point_stats);
    points_stats.setText(value);

应用程序崩溃。这是错误代码:
E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: de.chralt.circlesafe, PID: 6849
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{de.chralt.circlesafe/de.chralt.circlesafe.GameOverActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2691)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2752)
                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6120)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
                                                                    at de.chralt.circlesafe.GameOverActivity.onCreate(GameOverActivity.java:26)
                                                                    at android.app.Activity.performCreate(Activity.java:6664)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2644)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2752) 
                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6120) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

最佳答案

您在哪里尝试提取GameOverActivity中的点?由于您正在调用“SetResult”,因此它应该位于GameOverActivity的onActivityResult方法中。如果是这种情况,我认为应该看起来像这样:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (resultCode == Activity.RESULT_OK) {
    // TODO Extract the data returned from the GameActivity.
    int value = data.getIntExtra("key1", some_default_value);
  }
}

09-25 21:41