主要 Activity :
Intent intent = new Intent(Main.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
次要 Activity :
String value = getIntent().getStringExtra("name")
这里出了什么问题?我搜索了很多但没有成功......
谢谢
最佳答案
试试这个:
在主 Activity 中:
//Make sure Secondary is an Activity name. Secondary.class.
Intent intent = new Intent(MainActivity.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
在次要 Activity 中:
字符串值 = getIntent().getExtras().getString("name");
您需要先获取 bundle ,然后从中提取字符串。
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.getString("name");
}
两者都应该有效。第二个是检查包是否为空。
关于java - getStringExtra() 总是抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912163/