【问题标题】:Arraylist IndexOutOfBoundsException after removing element删除元素后的 Arraylist IndexOutOfBoundsException
【发布时间】:2014-08-28 01:42:17
【问题描述】:

我知道这是个愚蠢的问题,但我已经花了 1.5 小时查看代码但没有找到任何东西:

holder.removeQuestion
                    .setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            // Button b = (Button) v;
                            // Question question = (Question) b.getTag();
                            Log.i("before removing", questionList.get(0)
                                    .getQuestion());
                            questionList.remove(0);
                            dataAdapter.notifyDataSetChanged();
                            Log.i("after removing", questionList.get(0)
                                    .getQuestion());
                        }
                    });

这行:questionList.remove(0) 是问题所在,但为什么呢?列表中满是元素,我只想删除第一个,怎么会出站呢?我在想它可能会在稍后再次使用列表的代码中发生,也许我在某处说我期望的数组列表比我实际给出的更大。

07-07 13:24:08.005: I/before removing(16695): Ako?
07-07 13:24:08.005: I/after removing(16695): Sa?
07-07 13:24:08.009: V/ConvertView(16695): 0
07-07 13:24:08.009: V/ConvertView(16695): 1
07-07 13:24:08.013: V/ConvertView(16695): 2
07-07 13:24:08.017: D/AndroidRuntime(16695): Shutting down VM
07-07 13:24:08.017: W/dalvikvm(16695): threadid=1: thread exiting with uncaught     
exception (group=0xa62b1288)
07-07 13:24:08.021: E/AndroidRuntime(16695): FATAL EXCEPTION: main
07-07 13:24:08.021: E/AndroidRuntime(16695): java.lang.IndexOutOfBoundsException:     
Invalid index 2, size is 2
07-07 13:24:08.021: E/AndroidRuntime(16695):    at     
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-07 13:24:08.021: E/AndroidRuntime(16695):    at 
java.util.ArrayList.get(ArrayList.java:304)
07-07 13:24:08.021: E/AndroidRuntime(16695):    at 
com.example.discue.Discussion$MyCustomAdapter.getView

如果您还需要更多代码,我会添加评论。我不想过多地向您发送垃圾邮件。

编辑(onView 方法):

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) 
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.question_list, null);

            holder = new ViewHolder();
            holder.question = (TextView) convertView
                    .findViewById(R.id.question);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.likes = (TextView) convertView
                    .findViewById(R.id.numberOfLikes);
            // create button for deleting question (for speaker)
            holder.removeQuestion = (Button) convertView
                    .findViewById(R.id.deleteQuestionBtn);
            convertView.setTag(holder);
            // set onclick listener on remove question button
            holder.removeQuestion
                    .setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            // Button b = (Button) v;
                            // Question question = (Question) b.getTag();
                            Log.i("before removing", questionList.get(0)
                                    .getQuestion());
                            questionList.remove(0);
                            dataAdapter.notifyDataSetChanged();
                            Log.i("after removing", questionList.get(0)
                                    .getQuestion());
                        }
                    });
            // set onclick listener on like check box for question
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Question question = (Question) cb.getTag();
                    if (cb.isChecked()) {
                        // update number of likes under like button
                        question.setLikes(question.getLikes() + 1);

                    } else {
                        question.setLikes(question.getLikes() - 1);
                    }

                    // resort the arraylist of questions
                    Collections.sort(questionList,
                            new Comparator<Question>() {
                                public int compare(Question q1, Question q2) {
                                    return (q2.getLikes() - q1.getLikes());
                                }
                            });
                    // execute the update
                    dataAdapter.notifyDataSetChanged();

                    question.setSelected(cb.isChecked());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Question question = questionList.get(position);
        // show/hide question remove button (for the first question)
        if (question.isRemoveButtonVisible()) {
            holder.removeQuestion.setVisibility(View.VISIBLE);
        } else {
            holder.removeQuestion.setVisibility(View.GONE);
        }
        // show/ hide question likes
        if (question.isLikeButtonVisible()) {
            holder.checkBox.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }
        // set checked/unchecked and disabled/not disabled like check boxes
        // (all except for the first question)
        if (question.isLikeDisabled()) {
            holder.checkBox.setChecked(true);
            holder.checkBox.setEnabled(false);
        } else {
            holder.checkBox.setChecked(question.isSelected());
            holder.checkBox.setEnabled(true);

        }
        holder.question.setText(question.getQuestion());
        holder.likes.setText(question.getLikes() + "");
        holder.checkBox.setTag(question);
        holder.removeQuestion.setTag(question);
        dataAdapter.notifyDataSetChanged();
        return convertView;

    }

【问题讨论】:

  • 您的错误出现在 MyCustomAdapter.getView,而不是 questionList.remove。请发布此方法实现。
  • java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 您在某处访问 2(通过变量或硬编码)。您发布的方法不是发生错误的代码。能发下相关代码吗?
  • 非常清晰的错误信息:java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2.
  • 既然你得到了所有的输出,这意味着它是从所有的代码中传递过来的。其他地方是错误。双击 logcat 行,其中之一将带您进入错误
  • 问题已编辑,提前谢谢。

标签: java android listview arraylist


【解决方案1】:

在getView中给你的位置应该考虑到数据的大小。

我的猜测是,当您创建适配器时,您会给它一个特定列表(问题列表),但稍后,您会在本地创建列表的新实例并在不更新的情况下对其进行修改适配器。

确保您对列表所做的任何更改都在同一个列表实例上,并且不会创建新的列表实例。

【讨论】:

  • 你是完全正确的。我创建新实例并通过构造函数传递arraylist。谢谢,我会解决它。
  • 那么您有没有机会将此设置为正确答案? :)
【解决方案2】:

错误发生在这里:

Question question = questionList.get(position);

position2,这超出了范围。您可能希望在此之前进行检查或阻止该方法获得等于或大于questionList 大小的position 值。看来你的清单在 U.I.比questionList 有更多的项目有问题

【讨论】:

  • 我认为你是对的,我遇到了另一个错误但我的问题已经消失了,一旦你告诉我为什么位置是 2,我就会标记为答案?职位是什么?我的意思是我知道它是一个参数,但你不知道我如何得到它,它是从哪里得到的?
  • @OndrejTokar 在您的应用程序中,前端(UI)上的列表是否由 2 个以上的项目组成?您是否在其他任何地方操纵这个问题列表?
  • 是的,如果你是这个意思,可以通过编辑文本将元素添加到列表中。
猜你喜欢
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 2012-10-17
  • 2013-02-24
相关资源
最近更新 更多