1

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

我的代码

甲级

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());
    }
4

1 回答 1

1

您没有将 a 传递Bundle给 Activity B,而是传递了一个String额外的。所以当你打电话时getExtras(),那里什么都没有。您可以简单地调用getStringExtra()自身Intent来获取您的字符串。

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

或者,您可以创建一个新Bundle的 ,将字符串打包到其中,然后使用 传递它putExtras(myBundle),但这对于单个字符串来说太过分了。

于 2013-09-18T18:27:33.223 回答