gpt4 book ai didi

android - 为什么不在 Activity 之间传递变量值?

转载 作者:行者123 更新时间:2023-11-30 03:22:28 24 4
gpt4 key购买 nike

我是 android 开发的菜鸟,我正试图在两个类之间传递一个变量。根据我的调试器,该值是在第一类中设置的,但无论出于何种原因,它在第二类中返回 null。我最初尝试通过将变量设为公共(public)和静态来传递值,然后我尝试将值放在 Extras 中。在第二个 Activity 中,这两种方法都返回 null。我已经清理了项目但没有成功。似乎它不会不允许重置字符串的值。非常感谢任何解决此问题的帮助。

我的代码

A 级

case R.id.profile:      

selection = (Integer)v.getTag();
Log.e("Selection", String.valueOf(selection));

if(LandingActivity.user_isClient){
adapter_email = (listData.get(selection)).get("Email"); //<--debugger showing value as set here
Log.e("Adapter Email", adapter_email);
Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_BARBER_VIEW");
myIntent.putExtra("ADAPTER_EMAIL", adapter_email); //<--debugger showing value as set here too
c.startActivity(myIntent);
}else{
adapter_email = (listData.get(selection)).get("Client_Email"); //<--debugger showing value as set here
Log.e("Adapter Email", adapter_email);
Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_CLIENT_VIEW");
myIntent.putExtra("ADAPTER_EMAIL", adapter_email); //<--debugger showing value as set here too
c.startActivity(myIntent);
}

break;

B 级

barber_email_string ="";//Deubugger showing value as null and not "". WHY?
try{
if(barber_email_string.equals(null)||barber_email_string.equals("")){
barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");

if(barber_email_string.equals(null)||barber_email_string.equals("")){
barber_email_string = Uri.encode(MyAppointments_ListAdapter.adapter_email);
Log.e("BPV email",barber_email_string);
}
}
Log.e("BPV email",barber_email_string);
}catch(Exception e){
e.printStackTrace();
Log.e("On Create", e.toString());
}

最佳答案

您没有将 Bundle 传递给 Activity B,而是传递了一个 String extra。因此,当您调用 getExtras() 时,那里什么也没有。您可以简单地调用 Intent 本身的 getStringExtra() 来获取您的字符串。

barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");

或者,您可以创建一个新的Bundle,将字符串打包到其中,然后使用putExtras(myBundle) 传递它,但那是对单个字符串过度杀伤。

关于android - 为什么不在 Activity 之间传递变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18879174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com