Reputation: 2297
I'm using Intents, to pass an array from one Activity to another in Android. That's the code in one class.
Button los = (Button) findViewById(R.id.starten);
int[] a = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
shuffleArray(a);
final Intent intent;
intent = new Intent(this, melderfragen.class);
intent.putExtra("schuffledNumbers", a);
los.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
Here is the code in the recieving class.
Intent intent = getIntent();
int[] questions = intent.getIntArrayExtra("shuffledNumbers");
So far it works fine. The app crashes however, when I want to acces a number from the array like so:
int Fragenummer = questions[2];
What's wrong with my code? I hope I didn't ask a duplicate question, but I haven't found a solution here so far.
Upvotes: 0
Views: 69
Reputation: 81349
You are sending schuffledNumbers
and trying to read shuffledNumbers
. Note the extra c in the first one.
Its a good pratice to use constants to prevent this kind of situation. So you could define:
public static final String EXTRA_SHUFFLED_NUMBERS = "shuffled_numbers";
and use such constant at both places.
Upvotes: 3