这是我尝试在两个活动之间传递变量后的错误
示例:
主要活动
Intent KMH = new Intent(Options.this,VolumeControlMAIN.class);
Bundle DistanceValue = new Bundle();
DistanceValue.putBoolean("distance", DistanceTX);
KMH.putExtras(DistanceValue);
startActivity(KMH);
第二项活动
Bundle distanceK = getIntent().getExtras();
value = distanceK.getBoolean("distance");
这是我的日志猫。
08-28 11:36:18.923: E/AndroidRuntime(841): FATAL EXCEPTION: main
08-28 11:36:18.923: E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audiovolumecontrol/com.example.audiovolumecontrol.VolumeControlMAIN}: java.lang.NullPointerException
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.os.Looper.loop(Looper.java:137)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-28 11:36:18.923: E/AndroidRuntime(841): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 11:36:18.923: E/AndroidRuntime(841): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 11:36:18.923: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-28 11:36:18.923: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-28 11:36:18.923: E/AndroidRuntime(841): at dalvik.system.NativeStart.main(Native Method)
08-28 11:36:18.923: E/AndroidRuntime(841): Caused by: java.lang.NullPointerException
08-28 11:36:18.923: E/AndroidRuntime(841): at com.example.audiovolumecontrol.VolumeControlMAIN.onCreate(VolumeControlMAIN.java:144)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.Activity.performCreate(Activity.java:5104)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-28 11:36:18.923: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-28 11:36:18.923: E/AndroidRuntime(841): ... 11 more
我是否必须为我想要解析的每个变量创建一个intent,或者我可以使用全局意图并一次性传递所有这些?
任何人都可以确定我的日志猫要求我做什么吗?
答案 0 :(得分:0)
Intent i = new Intent(getBaseContext(), NameOfActivity.class);
i.putExtra("my_boolean_key", myBooleanVariable);
startActivity(i)
检索第二个活动:
Bundle bundle = getIntent().getExtras();
boolean myBooleanVariable = bundle.getBoolean("my_boolean_key");
使用上面的代码。
答案 1 :(得分:0)
在第一项活动中使用此代码:
Intent KMH = new Intent(Options.this,VolumeControlMAIN.class);
KMH.putExtras(“distance”,DistanceTX);
KMH.putExtras(“anotherValueKey”,“另一个值”);
startActivity(KMH);
并在第二个活动中使用此代码
Intent in=getIntent();
boolean value = in.getBoolean("distance");