我在使用包含按钮的列表视图的活动中遇到一个非常奇怪的问题。
错误似乎随机弹出(虽然我们知道它并不存在),我的意思是说我无法获得一个定义的模式来重现错误。它经常发生。
我试图解决这个问题的一个大问题是logCat在我的代码中没有显示错误,只是在(我相信)android用于确定点击来自的视图的代码中。请注意,错误中没有提到不属于android / java的包。
logcat的:
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);
}
}
anybode是否有任何想法可能是什么?我厌倦了试图弄清楚这一点,没有。我发现的大多数都是类似问题的错误报告,但我怀疑它是一回事,无论如何the report is here.
提前致谢Stefano
编辑:
错误弹出之前的LogCat,怀疑它与位置集有什么关系。
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
对位置选择不执行任何操作。
所有spinner数组适配器都是这样创建的:
arrayAdapterNAME = new ArrayAdapter<String>(this, R.layout.layoutspinner, DATA);
我会假设错误是因为((Spinner) (findViewById(R.id.spinnerB))).setSelection(t.posSB);
在spinnerB.setAdapter(arrayAdapterNAME)
完成之前执行了吗?
答案 0 :(得分:3)
此问题与ListView's
适配器无关,而与Spinner
适配器有关。
当您点击其中一个按钮时,您会在两个微调器上调用setSelection()
。如果你检查AbsSpinner.setSelection(int)
中的代码,你会看到它缓存了新选择项的整数索引,并调用requestLayout()
重绘自己(稍后一点)。
当UI线程处理布局消息时,它会布局视图树中的所有内容,但在布置Spinner时会爆炸,因为之前缓存的索引不再有效。有些东西改变了适配器的列表......这必须发生在响应UI事件(例如Button onClick())并且在处理布局消息之前得到处理。
以后添加的评论和代码添加到O.P。:
错误的假设是Spinner.setSelection()
会导致onItemSelectedListener
事件在返回之前运行。此特定问题的修复是从按钮onClick()设置微调器适配器,然后设置选定的索引。