这是我在第一个 Activity 中的代码:
Intent i = new Intent(this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
其中 first、second 是我想要传递的值。 在我的另一个类上,我有这个:
Intent i = getIntent();
Bundle extras = i.getExtras();
String result = extras.getString("id1");
System.out.println("yeah"+result);
但是在我运行它之后,我的结果是空的。你能帮帮我吗? 如果我以这种方式编写 getString,则会出现语法错误。
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);
}
关于android - getIntent.getExtras.getString() 中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8822553/