简体   繁体   English

在Android中通过意图传递捆绑时在Main活动上获取捆绑

[英]Getting bundle on Main activity when passing bundles by intent in android

So the problem I am having is that my app keeps crashing on launch, I have two activities. 所以我遇到的问题是我的应用在启动时一直崩溃,我有两个活动。 Activity A and Activity B. My app launches on Activity A but I have created a bundle in Activity B and I am sending it to Activity A. So when it launches the bundle is empty or null so it crashes, how do i fix this? 活动A和活动B。我的应用程序在活动A上启动,但是我在活动B中创建了一个包,并将其发送给活动A。因此,当它启动时,该包为空或为null从而崩溃了,我该如何解决? thanks. 谢谢。

This is in Activity A (Launching Activity) in on create 这是在活动A(启动活动)中创建的

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

Then we have me creating the bundle in Activity B 然后让我在活动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);

I would suggest the following: 我建议以下内容:

A. Use Bundle from Intent: A.从意图使用捆绑包:

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

B. Create a new Bundle: B.创建一个新的捆绑包:

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

C. Use putExtra() method of the Intent: C.使用Intent的putExtra()方法:

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

Finally in the launched Activity, you can read them hrough: 最后,在启动的活动中,您可以通读它们:

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

I just used Strings as an example for passing, I refer to Bundle and Intent . 我只是使用Strings作为传递的示例,我指的是BundleIntent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM