脑死亡时刻:通过Android的意图传递捆绑

时间:2013-08-02 01:25:15

标签: java android android-intent bundle

所以我试图从启动活动的意图中检索一个包时得到一个空指针异常。

上一个活动中的代码是:

Bundle b = new Bundle();
Intent that = new Intent(this, GoogleMapSearchActivity.class);
that.putExtra("bundle", b);
startActivity(that);

被调用的活动尝试使用以下命令查找包:

Bundle bund;
Intent activity = getIntent();
bund = activity.getBundleExtra("bundle");

然后空指针异常指向最后一行,然后是我脸上的一个非常愚蠢的表情,心里感到沮丧。

08-02 01:32:43.249: E/AndroidRuntime(2162): FATAL EXCEPTION: main
08-02 01:32:43.249: E/AndroidRuntime(2162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.donatorproj/com.example.donatorproj.GoogleMapSearchActivity}: java.lang.NullPointerException
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.os.Looper.loop(Looper.java:137)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at java.lang.reflect.Method.invoke(Method.java:511)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at dalvik.system.NativeStart.main(Native Method)
08-02 01:32:43.249: E/AndroidRuntime(2162): Caused by: java.lang.NullPointerException
08-02 01:32:43.249: E/AndroidRuntime(2162):     at com.example.donatorproj.GoogleMapSearchActivity.addInitialDataMarkers(GoogleMapSearchActivity.java:85)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at com.example.donatorproj.GoogleMapSearchActivity.onCreate(GoogleMapSearchActivity.java:78)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.Activity.performCreate(Activity.java:5104)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-02 01:32:43.249: E/AndroidRuntime(2162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-02 01:32:43.249: E/AndroidRuntime(2162):     ... 11 more

4 个答案:

答案 0 :(得分:2)

使用捆绑的程序:

意图捆绑:

Intent mIntent = new Intent(this, A.class);

Bundle extras = mIntent.getExtras();

extras.putString(key, value);

创建包

Intent intent = new Intent(this, A.class);

Bundle mBundle = new Bundle();

mBundle.extras.putString(key, value);

intent.putExtras(mBundle);

使用PutExtra方法

Intent intent = new Intent(this, A.class);

intent.putExtra(key, value);

然后,在启动的Activity中,您将通过以下方式阅读它们:

String values = getIntent().getExtras().getString(key)

答案 1 :(得分:0)

可能是因为你的包是空的。尝试使用hasExtra("bundle")检查它是否包含。

答案 2 :(得分:0)

我只想列出各个参数,例如

Intent intent = new Intent (SomeActivity.this, AnotherActivity.class);
    intent.putExtra("paramName1", theString);
    intent.putExtra("paramName2", "anotherString");
    startActivity(intent);

然后,在已启动的活动中,

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        variable1 = extras.getString("paramName1");
        variable2 = extras.getString("paramName2");
    }

你传递的一个参数,或整个包,必须包含第78行所见的空值。This SO post可能会解释这个问题。 希望这会有所帮助。

答案 3 :(得分:0)

这样做了:

Bundle b = new Bundle();
Intent that = new Intent(this, GoogleMapSearchActivity.class);
that.putExtras(b); // formerly that.putExtra("bundle", b);
startActivity(that);

Bundle bund;
Intent activity = getIntent();
bund = activity.getExtras(); // fomerly bund = activity.getBundleExtra("bundle");

通过简单地使用putExtras方法而不是putExtra然后使用keyname调用getBundleExtra,我的问题就消失了。我真的不明白为什么这是我做的第一种方式不会起作用。