OnCheckedChangedListener错误

时间:2015-08-08 17:57:16

标签: android listview

我不确定我在这里做错了什么。在下面获取indexOutOfBounds错误。它来自我的adapter.java,我不是100%它都是正确的。我认为有一些与获得我不熟悉的无线电组状态相关的部分。任何帮助表示赞赏。

08-08 13:36:25.939    1930-1930/com.example.x.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.x.myapp, PID: 1930
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.set(ArrayList.java:481)
        at com.example.x.myapp.ListCarsAdapter$1.onCheckedChanged(ListCarsAdapter.java:80)
        at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
        at android.widget.RadioGroup.access$600(RadioGroup.java:54)
        at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:155)
        at android.widget.CompoundButton.toggle(CompoundButton.java:112)
        at android.widget.RadioButton.toggle(RadioButton.java:78)
        at android.widget.CompoundButton.performClick(CompoundButton.java:124)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的adapter.java代码是:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    final ViewHolder holder;
    if(v == null) {
        v = mInflater.inflate(R.layout.list_item_car, parent, false);
        holder = new ViewHolder();
        holder.txtCarName = (TextView) v.findViewById(R.id.txt_car_name);
        holder.radioGroup = (RadioGroup) v.findViewById(R.id.scale);

        holder.radioGroup.setTag(position);
        v.setTag(holder);
    }
    else {
        holder = (ViewHolder) v.getTag();
    }

    // fill row data
    final Car currentItem = getItem(position);
    if(currentItem != null) {
        holder.txtCarName.setText(currentItem.getCar());
        holder.radioGroup.setTag(currentItem.getAnswer());
    }

    holder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        List<Integer> x = new ArrayList<Integer>();

        public void onCheckedChanged(RadioGroup group, int checkedId) {
            x.set(position, checkedId);

            for (int i = 0; i < x.size(); i++) {
                Log.v("Log", "" + x.get(i));
             }
       }
    });
    return v;
}

2 个答案:

答案 0 :(得分:2)

错误在本部分

List<Integer> x = new ArrayList<Integer>();
x.set(position, checkedId);

列表大小为0,因此您无法替换索引position上的项目,因为它超出范围。

答案 1 :(得分:1)

你的数组是空的。

IndexOutOfBoundsException: Invalid index 0, size is 0

这是失败的:

for (int i = 0; i < x.size(); i++) {
       Log.v("Log", "" + x.get(i));
    }

List<Integer> x = new ArrayList<Integer>();未初始化为:

 x.set(position, checkedId);

应该是:

x.Add(postion, checkedId);