【问题标题】:How to fix ArrayIndexOutOfBoundsException when a activity is reloaded in Android?在Android中重新加载活动时如何修复ArrayIndexOutOfBoundsException?
【发布时间】:2015-11-11 10:17:54
【问题描述】:

我以编程方式创建了 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.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            finish();
            startActivity(intent);
        }
    });

谢谢!

【问题讨论】:

  • 请提供所有logcat
  • 它在 logcat 中显示了什么
  • 是的,你是对的。这是我的entire logcat
  • 也许questions 数组比answerGroup 数组大。您正在通过questions 数组的每次迭代更改i,但您正在使用它来访问answerGroup 数组中的元素。 answer 数组也是如此。
  • 您的问题列表的大小是多少?看起来超过4。如果超过4,那么它会分配j=4,这会给出一个arrayindexoutofboundsexception。

标签: java android loops android-activity onclicklistener


【解决方案1】:

活动可能正在尝试引用旧视图元素(从活动重新启动之前开始)。尝试添加您的视图组件:

  • 无线电组
  • 单选按钮

作为类变量。你能试试这个并告诉我它在做什么吗?我会根据你给我的输入更新这个答案。

编辑:

我假设您使用ArrayList<Question>ArrayList<Answer> 作为questionsanswers 变量。

      for (Question qn : questions) {
            RadioGroup answerGroup = new RadioGroup(this);
            answerGroup.setOrientation(RadioGroup.VERTICAL);
            for (Answer an : qn.getAnswers()) {
                if (qn.getID() == an.getQuestion_id_answer()) {
                    RadioButton answer = new RadioButton(this);
                    answer.setText(an.getAnswer());
                    answerGroup.addView(answer);
                }
            }
            linearLayout.addView(answerGroup);
        }

也许您应该为您的问题添加一个 getter 方法,称为:getAnswers();,并为您的 Question 模型添加一个变量,称为 List<Answer> answers。当然,在您尝试对它们进行任何操作之前,需要设置这些答案。

【讨论】:

  • 好的,我会尝试但是如何添加我的视图组件?你能说得更具体点吗?
  • 难道不能在你的布局文件中创建这些视图吗?如果您重新启动活动,您的视图元素的生命周期将由 Android 框架处理。还有另一种可能性:您的questions 变量来自哪里?活动重启后是否清除?
  • 是的,我正在使用:List<Question> questions = db.getAllQuestions();List<Answer> answers = db.getAllAnswers();,但如果按照你说的做,我会收到此错误:java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
  • @AlexM。我的问题是你每个问题有超过 4 个答案,如果你的数据库中有重复的条目,这可能会发生。当Activity 开始时,您总是在数据库中添加条目,这意味着当活动重新开始时,它将为每个问题添加另外 4 个答案,这将使每个问题有 8 个答案。
  • 好的,我还有一个问题要问你。为什么不先在没有数据库的情况下尝试一下。只需在每次活动开始时回答和提问。如果可行,请尝试使用数据库。尝试从简单开始。这主要是应用程序开发和软件工程整体的方式。
【解决方案2】:

您必须调试您的代码,可能并非所有您的问题都有 4 个答案。

如果您的某个问题有超过 4 个答案(例如:5 个),并且您尝试遍历它们,您会得到 ArrayIndexOutOfBoundsException,因为 j>=4


作为解决方案:尝试为您的RadioButtons 使用List

List<RadioButton> answer = new ArrayList<RadioButton>();

并且为每次迭代添加一个新的RadioButton

answer.add(new RadioGroup(this));

PS:另外我建议您使用recreate() 方法重新加载/重新创建您的活动。

【讨论】:

  • 我所有的问题肯定有 4 个答案。我正在从数据库中提取这些问题和答案。如果每个问题有超过 4 个答案,那么我肯定首先遇到了这个问题。我试图这样做,但模拟器在启动时崩溃了。确实,为了简单起见,这个例子只有 5 个问题,有 4 个答案,但现在至少有 25 个问题,而不是 5 个问题,但答案的数量将保持不变,4。你能不能给我写一些根据我的例子行?我不完全理解你。我在onClick 中也使用recreate() 方法吗?泰
猜你喜欢
  • 2011-03-04
  • 2012-06-13
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多