0

I'm trying to pass a String array from one activity to another but when I try to read the array in the second activity, the values are null.

Here is how i'm passing the array from the first activity:

Bundle bundle = new Bundle();
bundle.putStringArray("Array", createArray(text));
Intent itemIntent = new Intent(this,Details.class); 
itemIntent.putExtra("passedArray", bundle);
startActivity(itemIntent);

createArray(text) is a method that returns an array.

Here's how i'm trying to read the array in the second activity:

Bundle extras = this.getIntent().getExtras();   
String[] array = extras.getStringArray("Array");

How do I initialise the array in the second activity with the corresponding array values that have been passed to it? If I try and read any of the values they have not been initialised and are null.

user1362255
  • 25
  • 2
  • 4

3 Answers3

1

Don't use bundle try this and first calculate your array:

itemIntent.putExtra("passedArray", createArray(text));
startActivity(itemIntent);

And receive them as

 String[] array = this.getIntent().getStringArray("passedArray");
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • Had to use `Bundle extras = this.getIntent().getExtras(); String[] array = extras.getStringArray("passedArray");` to receive the array but apart from that it worked. Thankyou! – user1362255 Apr 30 '12 at 16:04
0

You are nesting the bundle containing your array within another extra. That means you'd have to get the "passedArray" extra (which is a bundle) and then get your "Array" from the bundle that you just extracted. Instead change your code to this

Intent itemIntent = new Intent(this,Details.class); 
itemIntent.putExtra("Array", createArray(text));
startActivity(itemIntent);
dymmeh
  • 22,247
  • 5
  • 53
  • 60
0

I think you are messing around with bundle and extras. Look at this question. I think it can help you Sending arrays with Intent.putExtra

Community
  • 1
  • 1
nax83
  • 274
  • 3
  • 15