expandablelistview ArrayIndexOutOfBoundsException

时间:2015-06-15 09:48:20

标签: android expandablelistview

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

@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)

希望快速回答​​。 非常感谢提前 奥马

1 个答案:

答案 0 :(得分:7)

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

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

希望这会有所帮助。