简体   繁体   English

活动之间的Android Studio移交变量

[英]Android Studio Handover variables between activities

I´ve got one task. 我有一项任务。 I have to put the points of a game from GameActivity to the GameOverActivity. 我必须将游戏的要点从GameActivity放到GameOverActivity。 I tried for hours, but it doesn´t work... Here is my try: 我尝试了几个小时,但没有用...这是我的尝试:

This is in my GameActivity (put an extra): 这是在我的GameActivity中(额外输入):

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

And this is my GameOverActivity: 这是我的GameOverActivity:

    Bundle bundle = getIntent().getExtras();
    int value = bundle.getInt("key1");
    TextView points_stats = (TextView) findViewById(R.id.point_stats);
    points_stats.setText(value);

The Application crashes. 应用程序崩溃。 This is the error code: 这是错误代码:

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) 

Where are you trying to extract the points in your GameOverActivity? 您在哪里尝试提取GameOverActivity中的点? Since you are calling "SetResult" it should be inside your GameOverActivity's onActivityResult method. 由于您正在调用“ SetResult”,因此它应该位于GameOverActivity的onActivityResult方法中。 If this is the case I believe it should look something like this: 如果是这种情况,我认为应该看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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