gpt4 book ai didi

android - 奇怪的 IndexOUBException 在 ListView 中使用按钮

转载 作者:搜寻专家 更新时间:2023-11-01 09:16:19 25 4
gpt4 key购买 nike

我在使用包含按钮的 ListView 的 Activity 中遇到一个非常奇怪的问题。

错误似乎是随机弹出的(虽然我们知道这并不真正存在),我的意思是说我无法获得定义的模式来重现错误。它只是偶尔发生。

我试图弄清楚的最大问题是 logCat 在我的代码中没有显示错误,只是在(我相信)android 用来确定点击来源的 View 的代码中。请注意,错误中没有提到不属于 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);
}
}

有人知道这可能是什么吗?我厌倦了试图解决这个问题,但一无所获。我发现的大多数是类似问题的错误报告,但我怀疑它是同一件事,无论如何 the report is here.

提前致谢,斯特凡诺

编辑:

刚才报错的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 对位置选择不执行任何操作。所有微调器数组适配器都是这样创建的:

arrayAdapterNAME = new ArrayAdapter<String>(this, R.layout.layoutspinner, DATA);

我认为错误发生是因为 ((Spinner) (findViewById(R.id.spinnerB))).setSelection(t.posSB);spinnerB.setAdapter( arrayAdapterNAME)完成了吗?

最佳答案

此问题与 ListView 的 适配器无关,而与 Spinner 适配器有关。

当您单击其中一个按钮时,您会在两个微调器上调用 setSelection()。如果您检查 AbsSpinner.setSelection(int) 中的代码您会看到它缓存了新选择的项目的整数索引并调用 requestLayout() 重绘自身(在稍后的某个时间点)。

当 UI 线程处理布局消息时,它会在 View 树中布置所有内容,但在布置 Spinner 时会爆炸,因为它之前缓存的索引不再有效。某些东西改变了适配器的列表……这一定是响应 UI 事件(例如 Button onClick())而发生的,并且在处理布局消息之前得到处理。

稍后添加以下评论讨论和代码添加到 O.P.:

错误的假设是 Spinner.setSelection() 会导致 onItemSelectedListener 事件在返回之前运行。这个特定问题的修复是从按钮 onClick() 设置微调器适配器,然后设置选定的索引。

关于android - 奇怪的 IndexOUBException 在 ListView 中使用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4441883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com