在使用包含按钮的列表视图的活动中,我遇到了一个非常奇怪的问题。
这个错误似乎是随机出现的(尽管我们知道这并不存在),我的意思是说我不能得到一个定义的模式来重现这个错误。只是经常发生。
我要解决的一个大问题是logcat在我的代码中没有显示错误,只是在android用来确定点击来源的视图的代码中。请注意,错误中没有提到不属于android/java的包。
日志:

12-14 12:04:20.994: ERROR/AndroidRuntime(5619): FATAL EXCEPTION: main
12-14 12:04:20.994: ERROR/AndroidRuntime(5619): java.lang.IndexOutOfBoundsException
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at java.util.Arrays$ArrayList.get(Arrays.java:77)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.Spinner.makeAndAddView(Spinner.java:189)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.Spinner.layout(Spinner.java:148)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.Spinner.onLayout(Spinner.java:112)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1238)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1044)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.View.layout(View.java:7032)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1055)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1737)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.os.Looper.loop(Looper.java:123)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at java.lang.reflect.Method.invoke(Method.java:521)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-14 12:04:20.994: ERROR/AndroidRuntime(5619):     at dalvik.system.NativeStart.main(Native Method)

这是我的自定义arrayadapter代码,当我从不同的行中反复单击不同的“boton_modificar”时会发生错误,大多数情况下,如果我快速单击它们。
private class CustomAdapter extends ArrayAdapter<DataType> {

  public CustomAdapter(Context context, int resource,List<DataType> objects) {
   super(context, resource, objects);
  }

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

   final int position2 = position;

   if (row == null) {
    LayoutInflater inflater = getLayoutInflater();
    row = inflater.inflate(R.layout.row_layout,parent,false);
   }

   //Some more code here

   ImageButton boton_modificar = (ImageButton) row.findViewById(R.id.modificar);
   boton_modificar.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
     DataT t = tr.get(position2);
     ((EditText) (findViewById(R.id.monto))).setText(Double.toString(t.m));
     ((Spinner) (findViewById(R.id.spinnerTipoB))).setSelection(t.posSTB);
     ((Spinner) (findViewById(R.id.spinnerB))).setSelection(t.posSB);
    }
   });
   return (row);
  }
 }

有人知道这可能是什么吗?我厌倦了想办法解决这个问题。我发现的大多数是一个类似问题的bug报告,但我怀疑这是同一回事,无论如何the report is here.
提前谢谢你,斯特凡诺
编辑:
在错误弹出之前,怀疑它与位置集有关。
12-16 15:12:56.159: INFO/position2(21828): 7
12-16 15:12:56.159: INFO/Spinner A # elements:(21828): 3
12-16 15:12:56.159: INFO/Assigned position(21828): 1
12-16 15:12:56.159: INFO/Spinner B # elements:(21828): 20
12-16 15:12:56.159: INFO/Assigned position(21828): 5

添加代码:
spinnerTipoB.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
                        int posId = tipoBIds[position];
                        if (posId == 0) {
                                spinnerB.setAdapter(arrayAdapterBP);
                        } else if (posId == 1) {
                            spinnerB.setAdapter(arrayAdapterBT);
                        } else if (posId == 2) {
                            spinnerB.setAdapter(arrayAdapterBO);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // Do Nothing
                    }
                });

spinnerB在位置选择上不执行任何操作。
所有微调器阵列适配器的创建方式如下:
arrayAdapterNAME = new ArrayAdapter<String>(this, R.layout.layoutspinner, DATA);

我假设错误是因为((Spinner) (findViewById(R.id.spinnerB))).setSelection(t.posSB);是在spinnerB.setAdapter(arrayAdapterNAME)完成之前执行的?

最佳答案

这个问题与ListView's适配器无关,与Spinner适配器无关。
当您单击其中一个按钮时,您会在两个微调器上调用setSelection()。如果您检查AbsSpinner.setSelection(int)中的代码,您将看到它缓存新选定项的整数索引,并调用requestLayout()来重新绘制自身(稍后的某个点)。
当ui线程处理布局消息时,它会在视图树中布局所有内容,但在布局微调器时会弹出,因为它先前缓存的索引不再有效。有东西改变了适配器的列表…这必须是响应ui事件(如button onclick())而发生的,该事件在处理布局消息之前已被处理。
稍后添加以下评论讨论和添加到O.P.的代码:
错误的假设是Spinner.setSelection()会导致onItemSelectedListener事件在返回之前运行。解决此特定问题的方法是从button onclick()设置微调器适配器,然后设置选定的索引。

10-07 17:39