5

所以我遇到的问题是我的应用程序在启动时不断崩溃,我有两个活动。Activity A 和 Activity B。我的应用程序在 Activity A 上启动,但我在 Activity B 中创建了一个捆绑包,并将其发送到 Activity A。因此,当它启动捆绑包时为空或 null 所以它崩溃了,我该如何解决这个问题?谢谢。

这是在创建活动 A(启动活动)中

    Bundle extras = getIntent().getExtras();
    Title = extras.getString("Title");
    Description = extras.getString("Description");
    Price = extras.getString("Price");
    Availability = extras.getString("Availability");

然后我们让我在活动 B 中创建捆绑包

     Intent intent = new Intent(B.this, A.class);
                Bundle extras = new Bundle();
                extras.putString("Title", PostTitle);
                extras.putString("Description", PostDescription);
                extras.putString("Price", PostPrice);
                extras.putString("Availability", PostAvail);
                intent.putExtras(extras);
                startActivity(intent);
4

1 回答 1

10

我建议如下:

A. 从​​ Intent 使用 Bundle:

Intent pIntent = new Intent(this, JustaClass.class);
Bundle extras = pIntent.getExtras();
extras.putString(key, value); 

B. 创建一个新的 Bundle:

Intent pIntent = new Intent(this, JustaClass.class);
Bundle pBundle = new Bundle();
pBundle.putString(key, value);
mIntent.putExtras(pBundle);

C. 使用 Intent 的 putExtra() 方法:

Intent pIntent = new Intent(this, JustaClass.class);
pIntent.putExtra(key, value);

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

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

我只是以字符串为例进行传递,我指的是BundleIntent

于 2016-01-04T21:29:28.787 回答