在getIntent.getExtras.getString空值() [英] null value in getIntent.getExtras.getString()

查看:386
本文介绍了在getIntent.getExtras.getString空值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code在我的第一个活动:

 意向书我=新的意图(这一点,OtherScreen.class);
    i.putExtra(ID1,第一);
    i.putExtra(ID2,第二);
    startActivity(ⅰ);
 

,其中第一,第二是我想传递的价值观。 和我的其他类,我有这样的:

 意图I = getIntent();
        捆绑额外= i.getExtras();
        字符串结果= extras.getString(ID1);
        的System.out.println(是的+结果);
 

但我运行它之后,我的结果是null.Can你能帮我吗? 如果我写我的getString在这方面,我得到的语法错误。

  = extras.getString(ID1)字符串结果; // ID1不能被解析为一个变量
字符串结果= extras.getString(ID1,默认值); //删除参数
 

解决方案

 意向书我=新的意图(yourcurrentactivity.this,OtherScreen.class);

i.putExtra(ID1,第一);
i.putExtra(ID2,第二);
startActivity(ⅰ);

捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
    字符串结果= extras.getString(ID1);
    的System.out.println(是的+结果);
}
 

This is my code in my first activity:

Intent i = new Intent(this, OtherScreen.class);
    i.putExtra("id1", "first");
    i.putExtra("id2", "second");
    startActivity(i);

where first,second are the values I want to be passed. and on my other class i have this:

Intent i = getIntent();
        Bundle extras = i.getExtras();
        String result = extras.getString("id1");
        System.out.println("yeah"+result);

but after i run it, my result is null.Can you help me? If I write my getString in that ways, I am getting syntax errors.

String result = extras.getString(id1); //id1 cannot be resolved to a variable
String result = extras.getString("id1","default value"); // remove argument

解决方案

Intent i = new Intent(yourcurrentactivity.this, OtherScreen.class);

i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String result = extras.getString("id1");                
    System.out.println("yeah"+result);
}

这篇关于在getIntent.getExtras.getString空值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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