【问题标题】:How to pass data between 3 or more activities in Android Studio?如何在 Android Studio 中的 3 个或更多活动之间传递数据?
【发布时间】:2020-01-20 21:19:05
【问题描述】:

我正在尝试创建一个项目,在 MainActivity 上获取您的电话号码,并将其传递给 VerifyPhone 活动,该活动将身份验证代码发送到您的手机。

我只能将电话号码值从 MainActivity 传递给 VerifyPhone 活动,但无法将其转换为字符串值以将其传递给我的第三个活动。

我想做什么:

MainActivity信息通道:

String complete_phone_num = "+" + phone_num;
            Intent intent = new Intent(MainActivity.this, VerifyPhoneActivity.class);
            intent.putExtra("phonenumber", complete_phone_num);
            startActivity(intent);

验证电话活动:

    Bundle bundle = getIntent().getExtras();
    String phonenumber = bundle.getString("phonenumber");

然后我尝试将phonenumber 传递给ProfileActivity

 Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.putExtra("phoneNum", phonenumber);
                        startActivity(intent);

不幸的是,它无法识别 OnCreate 之外的变量 phonenumber

我希望将电话号码传递给其他活动,以便将其存储在我的数据库中并将其用于报表,以便对其进行组织。

【问题讨论】:

    标签: android android-studio android-intent


    【解决方案1】:

    我相信您正在尝试创建新意图并在 oncreate 方法之外启动它,但是您在其中声明了变量 phonenumber,因此无法超出其范围,请尝试将其声明为 a 的私有字段Activity 类并在 onCreate() 方法中初始化,然后就可以使用了。

    【讨论】:

    • 不,我在 OnCreate 方法中的 VerifyPhone 活动(第二个)中声明电话号码,并尝试将电话号码传递给 ProfileActivity(第三个)
    • 我相信 Madonabulia 所说的很可能是正确的 Ada,您很可能遇到了范围问题,即您的第三个活动不知道您的变量。所以我会做他的建议并尝试在 onCreate 方法之外声明你的变量,然后在你的 onCreate 方法中填充它,然后它应该可以被第三种方法访问。
    【解决方案2】:

    正如@Code-Apprentice 所说。我也认为你需要在 Method 之外添加一个变量,并将 extra 的值设置为这个变量。

    类似这样的:

    String phonenumber = "";
    
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getIntent().getExtras();
    phonenumber = bundle.getString("phonenumber");
    
    public void someOtherMethod(){
    Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.putExtra("phoneNum", phonenumber);
                        startActivity(intent);
    }
    

    另一种解决方案是将字符串作为参数传递给特定方法。

    【讨论】:

    【解决方案3】:
    intent.putExtra("phoneNum", phonenumber);
    

    确定您没有打错字并在 ProfileActivity 中检查“phoneNum”吗?

    如果不是,您所说的 OnCreate 之外是什么意思?您是否尝试将其作为成员变量进行访问?如果您这样做了,您应该等待 ProfileActivity 中的 OnCreate 使用您已在 VerifyPhone 中使用的方法先完成设置值

    【讨论】:

    • 是的,我已经检查过了,但事实并非如此。正如我所说:不幸的是,它无法识别 OnCreate 之外的可变电话号码。
    • 我回答的第二部分怎么样?您是否尝试在 onCreate() 之前访问它?因为只有在 onCreate 之后/中才能检索和使用该值
    • 我可以在 OnCreate 之外的第二个活动中声明“电话号码”吗?如果是这样,怎么做?因为它导致我的应用程序崩溃。
    • 只要在类中声明为变量就好了 class ProfileActivity{ String phonenumber; onCreate(){} } 然后在使用前检查phonenumber==null,并在oncreate中使用getIntent设置值
    【解决方案4】:

    很遗憾,它无法识别 OnCreate 之外的可变电话号码。

    您需要将phonenumber 声明为活动的私有字段。我建议你学习 Java 类中的实例字段。

    【讨论】:

    • 我只是作为高中最后一年的项目做的,我只需要基本的了解,如果你能给我解释一下,我会很感激。
    • @Ada 我已经给了你一些术语,你可以使用谷歌从 Java 或 Android 教程中获得基本的理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多