意图崩溃的应用程序

时间:2012-06-21 18:04:17

标签: java android arrays android-intent

我正在使用Intents,在Android中将数组从一个Activity传递到另一个Activity。 这是一个类中的代码。

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);

            }
        });

以下是接收课程中的代码。

Intent intent = getIntent();
        int[] questions = intent.getIntArrayExtra("shuffledNumbers");

到目前为止一切正常。然而,当我想从数组中访问数字时,应用程序崩溃了:

int Fragenummer = questions[2];

我的代码出了什么问题? 我希望我没有问过一个重复的问题,但到目前为止我还没有找到解决方案。

1 个答案:

答案 0 :(得分:3)

您正在发送schuffledNumbers并尝试阅读shuffledNumbers。请注意第一个中的额外 c

使用常量来防止这种情况是一个很好的实践。所以你可以定义:

public static final String EXTRA_SHUFFLED_NUMBERS = "shuffled_numbers";

并在两个地方使用这样的常数。