AAkira / ExpandableLayout

[Deprecated] An android library that brings the expandable layout with various animation. You can include optional contents and use everywhere.
Apache License 2.0
1.65k stars 325 forks source link

Code crashed if only empty ExpandableLayout added to xml #82

Closed ivankarpey closed 8 years ago

ivankarpey commented 8 years ago

I have markup which looks like:

<com.github.aakira.expandablelayout.ExpandableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/placeholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--<TextView xmlns:android="http://schemas.android.com/apk/res/android"-->
            <!--android:id="@+id/tv7778"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="@dimen/input_height"-->
            <!--android:background="@drawable/border11_bottom"-->
            <!--android:gravity="center_vertical"-->
            <!--android:text="WE NEVER FORGET WE NEVER FORGIVE"/>-->

    </com.github.aakira.expandablelayout.ExpandableLinearLayout>

and if I comment textview in the middle it's fails with:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: io.test PID: 3618
                  java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
                      at java.util.ArrayList.get(ArrayList.java:310)
                      at com.github.aakira.expandablelayout.ExpandableLinearLayout.onMeasure(ExpandableLinearLayout.java:126)
                      at android.view.View.measure(View.java:18788)
                      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                      at android.view.View.measure(View.java:18788)
                      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                      at android.view.Choreographer.doFrame(Choreographer.java:606)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ivankarpey commented 8 years ago

the problem is in this line:

https://github.com/AAkira/ExpandableLayout/blob/master/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLinearLayout.java#L126

You consider that array will have elements and not checking case when it could be empty. So trying ti get item with -1 index.

AAkira commented 8 years ago

@ivankarpey

Thanks a bunch! :-) I fixed on v1.6.0