在多个活动之间传递数据

时间:2011-11-29 18:20:06

标签: android android-activity

我有三个活动,比如我有来自ActivityA的数据,我希望ActivityC检索它,但它必须通过ActivityB,我不希望它对传递的数据做任何事情。反正有吗?

我试过了

ActivityA.java

Intent i = new Intent(this, ActivityB.class);
i.putExtra("DATA1", "Hello");
i.putExtra("DATA2", "World");
startActivity(i);

ActivityB.java对此没有任何作用,但必须在ActivityC之前看到它。

ActivityC.java

Bundle b = getIntent().getExtras();
String data1 = b.getString("DATA1");
String data2 = b.getString("DATA2");

但它给了我这个例外

11-30 02:12:05.165: W/dalvikvm(1134): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-30 02:12:05.185: E/AndroidRuntime(1134): FATAL EXCEPTION: main
11-30 02:12:05.185: E/AndroidRuntime(1134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.test/com.src.test.ActivityC}: java.lang.NullPointerException
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.os.Looper.loop(Looper.java:123)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at java.lang.reflect.Method.invoke(Method.java:521)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at dalvik.system.NativeStart.main(Native Method)
11-30 02:12:05.185: E/AndroidRuntime(1134): Caused by: java.lang.NullPointerException
11-30 02:12:05.185: E/AndroidRuntime(1134):     at com.src.test.ActivityC.onCreate(ActivityC.java:98)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

指向String data1 = b.getString("DATA1");

有没有办法正确地做到这一点?

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:6)

从B到C调用时,添加到您的意图:

myIntent.putExtras(getIntent().getExtras());

答案 1 :(得分:5)

这是因为您的Activity不会将其推送到Activity:

Intent newIntent = new Intent(ActivityB.this, ActivityC.class);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    newIntent.putExtras(bundle);
}
startActivity(newIntent);

答案 2 :(得分:2)

在活动B中执行此操作:

Bundle b = getIntent().getExtras();
String data1 = b.getString("DATA1");
String data2 = b.getString("DATA2");


Intent i = new Intent(this, ActivityC.class);
i.putExtra("DATA1", data1);
i.putExtra("DATA2", data2);
startActivity(i);