我试图在两个活动之间传递一个字符串.我在其他项目中使用相同的方法完成了这个,但由于某种原因,当我调用intent.getStringExtra(String)时,我得到一个NullPointerException.我也试过通过创建一个Bundle for extras
Bundle b = getIntent().getExtras();
Run Code Online (Sandbox Code Playgroud)
但那也返回了null.下面是我目前正在尝试使用的代码.
活动A:
Intent myIntent = null;
String select = "";
if (selection.equals("Chandelle")) {
myIntent = new Intent(Commercial.this, Chandelle.class);
select = "Chandelle";
} else if (selection.equals("Eights on Pylons")) {
myIntent = new Intent(Commercial.this, EightsOnPylons.class);
select = "Eights on Pylons";
}
// Start the activity
if (myIntent != null) {
myIntent.putExtra("selection", select);
Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
startActivity(myIntent);
}
Run Code Online (Sandbox Code Playgroud)
这是活动B中试图拉出额外内容的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == …Run Code Online (Sandbox Code Playgroud)