-5

这是我尝试在两个活动之间传递变量后的错误

例子:

主要活动

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

我是否必须为要解析的每个变量创建一个意图,或者我可以使用全局意图并一次通过所有变量?

谁能确定我的日志猫要我做什么?

4

2 回答 2

0

在您的第一个活动中使用此代码:

Intent KMH = new Intent(Options.this,VolumeControlMAIN.class);

KMH.putExtras("距离", DistanceTX);

KMH.putExtras("anotherValueKey","another Value");

开始活动(公里);

并在您的第二个活动中使用此代码

 Intent in=getIntent();

 boolean value = in.getBoolean("distance");
于 2013-08-28T12:07:25.570 回答
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");

使用上面的代码相同。

于 2013-08-28T11:58:19.087 回答