在android中通过意图传递包时在主要活动上获取包 [英] Getting bundle on Main activity when passing bundles by intent in android

查看:20
本文介绍了在android中通过意图传递包时在主要活动上获取包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到的问题是我的应用程序在启动时一直崩溃,我有两个活动.Activity A 和 Activity B.我的应用程序在 Activity A 上启动,但我在 Activity B 中创建了一个包,我将它发送到 Activity A.所以当它启动时包是空的或空的所以它崩溃了,我该如何解决这个问题?谢谢.

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? thanks.

这是在创建时的活动 A(启动活动)

This is in Activity A (Launching Activity) in on create

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

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

Then we have me creating the bundle in Activity 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);

推荐答案

我建议如下:

A.使用来自 Intent 的 Bundle:

A. Use Bundle from Intent:

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

B.创建一个新的捆绑包:

B. Create a new Bundle:

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

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

C. Use putExtra() method of the Intent:

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

最后在启动的Activity中,你可以通过阅读它们:

Finally in the launched Activity, you can read them hrough:

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

我只是以Strings为例进行传递,参考Bundle意图.

I just used Strings as an example for passing, I refer to Bundle and Intent.

这篇关于在android中通过意图传递包时在主要活动上获取包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆