如何修复在Android中崩溃的活动的重启?

时间:2015-11-11 00:03:43

标签: java android loops android-activity onclicklistener

我已经以编程方式创建了5个无线电组,每组有4个单选按钮。答案是数据库查询的结果。我添加了一个重启按钮和一个OnClickListener。我想当有人点击按钮,重新开始我的活动。当第一次启动应用程序时,它工作正常但是当我按下按钮重新加载活动时,我收到此错误:Unable to start activity ComponentInfo{...}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4这是怎么可能的,第一次完美地工作以及重新加载活动崩溃?我怎样才能解决这个问题? 这是我的代码:

answerGroup = new RadioGroup[5];
answer = new RadioButton[4];
int i = 0;
for (Question qn : questions) {
    answerGroup[i] = new RadioGroup(this);
    answerGroup[i].setOrientation(RadioGroup.VERTICAL);
    int j = 0;
    for (Answer an : answers) {
        if (qn.getID() == an.getQuestion_id_answer()) {
            answer[j] = new RadioButton(this);
            answer[j].setText(an.getAnswer());
            answerGroup[i].addView(answer[j]);
            j++;
        }
    }
    linearLayout.addView(answerGroup[i]);
    i++;
}

restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
linearLayout.addView(restartButton);

restartButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        startActivity(intent);
    }
});

谢谢!

1 个答案:

答案 0 :(得分:0)

In Intent intent = getIntent();你需要设置像

这样的参数
Intent intent = getIntent(getApplicationContext(),NameOfYourClass.class)

如果您使用SDK 11+调用

,请尝试重新创建活动
super.recreate();

但是,如果这两者都不起作用,我认为你的表中有一些错误。尝试使用ArrayList。