Android意图将getIntExtra设置为全局变量

时间:2017-07-29 11:36:10

标签: android android-intent

我有三个活动,Main,SecondActivity和ThirdActivity。我想从Main向SecondActivity发送int变量,之后,从Main(我得到)和SecondActivity发送int(number)到ThirdActivity。

MainActivity:

String Score = 0;

public void onNextClick(View view){
        Score++;
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("Answer", Score);
        startActivity(intent);
        finish();
    }

SecondActivity:

Intent intent = getIntent();
int Score2 = intent.getIntExtra("Answer", 0);

public void onNextClick(View view){
        Score2++;
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("Answer2", Score2);
        startActivity(intent);
        finish();
    }

但我得到错误:

  

java.lang.RuntimeException:无法实例化活动   ComponentInfo {com.kvizzadatak / com.kvizzadatak.SecondActivity}:   显示java.lang.NullPointerException                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2174)                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)                                                                    在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1246)                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)                                                                    在android.os.Looper.loop(Looper.java:212)                                                                    在android.app.ActivityThread.main(ActivityThread.java:5151)                                                                    at java.lang.reflect.Method.invokeNative(Native Method)                                                                    在java.lang.reflect.Method.invoke(Method.java:515)                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)                                                                    at dalvik.system.NativeStart.main(Native Method)                                                                 引起:java.lang.NullPointerException                                                                    在com.kvizzadatak.ThirdActivity。(ThirdActivity.java:23)                                                                    at java.lang.Class.newInstanceImpl(Native Method)                                                                    在java.lang.Class.newInstance(Class.java:1208)                                                                    在android.app.Instrumentation.newActivity(Instrumentation.java:1061)                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)                                                                    在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1246)                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)                                                                    在android.os.Looper.loop(Looper.java:212)                                                                    在android.app.ActivityThread.main(ActivityThread.java:5151)                                                                    at java.lang.reflect.Method.invokeNative(Native Method)                                                                    在java.lang.reflect.Method.invoke(Method.java:515)                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)                                                                    在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

如果要将任何内容放在可以全局轻松访问的位置,可以使用共享首选项或全局常量类。要使用Shared Pref,请执行以下操作

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("Answer2", score2);
editor.commit();

要访问它,您可以使用 -

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("Answer2", -1);

如果您想使用Intents进行转移,那么您的getIntent应该是Android生命周期方法的一部分。因此,您最好使用onCreate方法执行以下代码。

Intent intent = getIntent();
int Score2 = intent.getIntExtra("Answer", 0);