Android意图包总是空的?
我正在运行以下代码。 有两个片段。 第一个是我如何设置捆绑,第二个是我如何检索它。 出于某种原因,每次检查捆绑包时,它都会返回null。
关于为什么会这样或者我做错了什么的任何建议将不胜感激。
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(); }
当你创建你的意图时,只需将附加内容放在那里。 您正尝试在上面的代码中访问错误的包。 这样的事情应该有效。
Intent intent = new Intent(this, com.hom.app.Hom.class); intent.putExtras("WELL", "yes"); startActivity(intent);
不知道为什么它会返回null,但是你的代码不能正常工作,因为你正在使用==进行字符串比较
这一行:
if(well == standard) method();
应该
if(well.equals(standard)) method();
从文档 :
键必须包含包前缀,例如app com.android.contacts将使用“com.android.contacts.ShowAll”之类的名称。
所以相反,你为什么不用它
intent.putExtra("WELL", "yes");