1

因此,我在尝试从启动活动的意图中检索包时遇到空指针异常。

上一个活动中的代码是:

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

4 回答 4

2

使用bundle的流程:

捆绑意图:

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)
于 2013-08-02T01:43:45.827 回答
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 然后使用键名调用 getBundleExtra 就消失了。我真的不明白为什么这是我做的第一种方法,但它不起作用。

于 2013-08-02T02:31:08.130 回答
0

可能是因为您的捆绑包是空的。尝试使用hasExtra("bundle")来检查它是否有捆绑包。

于 2013-08-02T01:29:58.743 回答
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 行中的空值。这篇 SO 帖子可能会阐明这个问题。希望这可以帮助。

于 2013-08-02T01:33:00.907 回答