问题
So I am trying to launch a new activity after the item in a list is selected.... pretty basic based on what I've read. I am also trying to send a value in the extras. So I can select the item in a list, and the new activity starts, extras is set, but the value in the extras is empty. I've noticed the id of the intent on the new activity doesn't match the one from the 1st activity. I don't know if it is supposed to or not.
From Activity 1:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent displayIntent = new Intent(getApplicationContext(), DisplayActivity.class);
int index = _names.indexOf(((TextView) view).getText());
displayIntent.putExtra("ID_TAG", ids.get(index));
startActivity(displayIntent);
}
In Activity2 (DisplayActivity)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
_hiveIndex = extras.getLong("ID_TAG");
}
Any Ideas why I wouldn't be getting the value? The mMap under the extras is set to a hash map before in the 1st intent, but is null in activity2.
回答1:
displayIntent.putExtra("ID_TAG", ids.get(index)) of Activity1 not using Bundle object,to put Bundle object you need to use putExtras(Bundle bundle) method instead of it.Since you are trying to get the Bundle object in Actvitiy2. Your passing other than Bundle object in Activity1 putExtra method,but you trying to get the Bundle object in Activity2 for that reason your are getting nothing. displayIntent.putExtra("ID_TAG", ids.get(index));replace with displayIntent.putExtras(your bundle object);
Or you can use getIntExtra(String name, int defaultValue)
method.
回答2:
In Activity1 you store an Integer. In Activity2 you try to retrieve a Long. You either need to getInteger in Activity2 or store Long in Activity1. Understand?
回答3:
how about this?
in DisplayActivity,
use getIntent().getIntExtra("ID_TAG")
来源:https://stackoverflow.com/questions/6554977/intent-send-is-not-intent-received