gpt4 book ai didi

android - 可扩展 ListView ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 14:31:08 29 4
gpt4 key购买 nike

我正在尝试实现具有 2 种不同布局的 ExpandableListView,到目前为止,我花了一段时间才真正让 1 个 View 组使用一个布局,而其他 2 个 View 组使用另一个。到目前为止,外观是所需要的,但是当我在同一个 View 组或两个不同的 View 组上单击两次时,应用程序会因数组越界错误而崩溃。这是我的代码

@Override
public int getChildType(int groupPosition, int childPosition) {

int result;
if (groupPosition == 0)
{
Log.d("getChildType","Parent " + groupPosition + " detected. returning 0");
result = 0;
}
else
{
Log.d("getChildType","Parent " + groupPosition + " detected. returning 1");
result = 1;
}
return result;
}

@Override
public View getChildView(final int parentPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

Log.d("status","parent : "+parentPosition + "child : " + childPosition + "View : "+convertView + "ViewGroup : "+parent);
final String child_title = (String) getChild(parentPosition, childPosition);
int itemType = 5;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemType = getChildType(parentPosition,childPosition);

switch (itemType)
{
case 0:
convertView = inflater.inflate(R.layout.activities2_child_layout, parent, false);
break;
case 1:
convertView = inflater.inflate(R.layout.activities2_child2_layout, parent, false);
break;
}
}
if (itemType == 0)
{
TextView child_textview = (TextView) convertView.findViewById(R.id.activities2_child_txt);
final ImageView img_Likes = (ImageView) convertView.findViewById(R.id.activities2_img_like);
final ImageView img_Dislikes = (ImageView) convertView.findViewById(R.id.activities2_img_dislike);
child_textview.setText(child_title);
}
else if(itemType == 1)
{
TextView child_textview = (TextView) convertView.findViewById(R.id.activities2_child2_txt);
child_textview.setText(child_title);
}



return convertView;
}

错误

    06-15 11:54:14.753  29578-29578/android.omar.com.slidingtab E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
06-15 12:02:53.343 31025-31025/android.omar.com.slidingtab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41eb1378)
06-15 12:02:53.393 31025-31025/android.omar.com.slidingtab E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6444)
at android.widget.ListView.layoutChildren(ListView.java:1603)
at android.widget.AbsListView.onLayout(AbsListView.java:2106)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:953)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1687)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1545)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1450)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1687)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1545)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1450)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
at android.view.View.layout(View.java:13923)
at android.view.ViewGroup.layout(ViewGroup.java:4472)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1876)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1697)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4223)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)

希望得到快速答复。非常感谢
奥马尔

最佳答案

我认为你应该覆盖 getChildTypeCount() 并返回 2

   @Override
public int getChildTypeCount(){
return 2;
}

希望这会有所帮助。

关于android - 可扩展 ListView ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30842105/

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