2
我已經以編程方式創建了5個單選按鈕,每個單選按鈕都有4個單選按鈕。答案是數據庫查詢的結果。我添加了一個重啓按鈕和一個OnClickListener
。我想要有人點擊按鈕時,重新開始我的活動。當第一次應用程序啓動時,它工作正常,但當我按下按鈕重新加載活動,我得到這個錯誤:Unable to start activity ComponentInfo{...}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
這是如何可能,第一次完美工作,當重新加載活動崩潰?我怎樣才能解決這個問題? 這裏是我的代碼:如何解決在Android中崩潰的活動的重新啓動?
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);
}
});
謝謝!
謝謝你,但它不起作用。我得到這個錯誤:'得到活動意圖不能應用'。任何其他想法? –
更新回答 –
如何使用'super.recreate();'?而不是'startActivity(intent);'? –