1

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.

mibollma
  • 14,959
  • 6
  • 52
  • 69
Dman25
  • 13
  • 5
  • 2
    Welcome to Stackoverflow. To assist users in reading your post, please place code samples inside code blocks. – Mike Jul 02 '11 at 03:49

3 Answers3

2

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.

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
sunriser
  • 770
  • 3
  • 12
  • So this worked (thanks btw). I think I got a little mixed up with this and this [link]http://stackoverflow.com/questions/6100570/do-intent-extras-have-to-be-removed[link] – Dman25 Jul 02 '11 at 04:46
0

how about this?

in DisplayActivity,

use getIntent().getIntExtra("ID_TAG")

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
이수홍
  • 43
  • 1
  • 5
0

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?

Spidy
  • 39,723
  • 15
  • 65
  • 83
  • putExtra can take any of the primitive types, so I don't think that matters. Also, I originally used getInt, but had the same problem. I have the debugger stop before I do the getIntent even, and the problem already exists in the bundle. – Dman25 Jul 02 '11 at 04:17