安卓getIntent()。getExtras()返回null [英] Android getIntent().getExtras() returns null
问题描述
我想传递两个活动之间的字符串。我在用同样的方法等项目做到了这一点,但由于某种原因,我得到一个NullPointerException异常时,我呼吁intent.getStringExtra(字符串)。我也曾尝试通过
I'm trying to pass a string between two activities. I've done this in other projects using the same method, but for some reason I'm getting a NullPointerException when I call intent.getStringExtra(String). I have also tried creating a Bundle for the extras via
Bundle b = getIntent().getExtras();
但也返回null。下面是code,我目前正在尝试使用。
but that also returned null. Below is the code that I am currently trying to use.
活动答:
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);
}
这里的code在b活动,试图拉多余的:
Here's the code in activity B that tries to pull the extra:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
我已经试过pretty的传球额外的每得多另一种方式,但他们似乎都表现得这样。我在想什么?
I've tried pretty much every alternative way of passing extras, but they all seem to behave this way. What am I missing?
推荐答案
添加的ToString()
到 myIntent.putExtra(选择,选择);
,使其 myIntent.putExtra(选择,select.ToString());
这篇关于安卓getIntent()。getExtras()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!