繁体   English   中英

意向崩溃的应用程序

[英]intent crashing app

我正在使用Intents,将数组从一个Activity传递到Android中的另一个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];

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

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

使用常量来防止这种情况是一个好习惯。 因此,您可以定义:

public static final String EXTRA_SHUFFLED_NUMBERS = "shuffled_numbers";

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM