我是Android开发的菜鸟,我试图在两个类之间传递一个变量。根据我的调试器,该值在第一个类中设置,但无论出于何种原因,它在第二个类中返回null。我最初尝试通过使变量公共和静态传递值,然后我尝试将值放在Extras中。两个方法在第二个Activity中返回null。我没有成功清理项目。似乎它不会允许重置字符串的值。任何帮助解决这个问题都非常感谢。
我的代码
CLASS 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;
CLASS 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());
}
答案 0 :(得分:1)
您没有将Bundle
传递给活动B,而是传递了String
个额外费用。所以当你致电getExtras()
时,那里什么都没有。您只需致电getStringExtra()
本身Intent
即可获取字符串。
barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");
或者,你可以创建一个新的Bundle
,将字符串打包到那里,并用putExtras(myBundle)
传递它,但这对单个字符串来说太过分了。