Java Android意图包总是空的?
我正在运行下面的代码。这里有两个片段。第一个是如何设置包,第二个是如何检索包。出于某种原因,每次我检查bundle时,它都返回null 任何关于这是为什么或我做错了什么的建议都将不胜感激Java Android意图包总是空的?,java,android,Java,Android,我正在运行下面的代码。这里有两个片段。第一个是如何设置包,第二个是如何检索包。出于某种原因,每次我检查bundle时,它都返回null 任何关于这是为什么或我做错了什么的建议都将不胜感激 Intent intent = new Intent(this, com.hom.app.Hom.class); Bundle b = new Bundle(); b.putString("WELL", "yes"); intent.putExtras(b
Intent intent = new Intent(this, com.hom.app.Hom.class);
Bundle b = new Bundle();
b.putString("WELL", "yes");
intent.putExtras(b);
startActivity(intent);
正在检索捆绑包:
String well ="";
Bundle bun = getIntent().getExtras();
String standard = "yes";
if(bun != null){
Log.v("Bundle", "Contains data");
well = bun.getString("WELL");
if(well == null) well = "";
if(well == standard) method();
}
不知道为什么它会返回null,但是即使捆绑包正确通过,代码也不会工作,因为您正在与进行字符串比较== 这一行:
if(well == standard) method();
应该是
if(well.equals(standard)) method();
不知道为什么它会返回null,但是即使捆绑包正确通过,代码也不会工作,因为您正在与进行字符串比较== 这一行:
if(well == standard) method();
应该是
if(well.equals(standard)) method();
当你创造你的意图时,只需把额外的东西放在那里。您试图访问上面代码中的错误包。这样的办法应该行得通
Intent intent = new Intent(this, com.hom.app.Hom.class);
intent.putExtras("WELL", "yes");
startActivity(intent);
当你创造你的意图时,只需把额外的东西放在那里。您试图访问上面代码中的错误包。这样的办法应该行得通
Intent intent = new Intent(this, com.hom.app.Hom.class);
intent.putExtras("WELL", "yes");
startActivity(intent);
从:
密钥必须包含一个包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称
那么,你为什么不直接使用
intent.putExtra("WELL", "yes");
从:
密钥必须包含一个包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称
那么,你为什么不直接使用
intent.putExtra("WELL", "yes");
antlersoft关于字符串比较的说法也是正确的。我已经改变了字符串比较。新手犯了个错误!我还将代码修改为上面推荐的代码,但它仍然返回bundle是空的。我在很多地方都这样做了,它仍然有效。您是否找到了解决方案,或者是否希望扩展代码以获得进一步帮助?antlersoft关于字符串比较的说法也是正确的。我已经更改了字符串比较。新手犯了个错误!我还将代码修改为上面推荐的代码,但它仍然返回bundle是空的。我在很多地方都这样做了,它仍然有效。您是否找到了解决方案,或者是否希望扩展您的代码以获得进一步帮助?您是否曾经使用过此功能?您是否曾经使用过此功能?