zyyoona7 / WheelPicker

A smooth, highly customizable wheel view and picker view, support 3D effects like iOS. 一个顺滑的、高度自定义的滚轮控件和选择器,支持类似 iOS 的 3D 效果
Apache License 2.0
825 stars 97 forks source link

出现“divide by zero”错误并闪退了 #20

Closed bkleopold closed 5 years ago

bkleopold commented 5 years ago

写了个小Demo测试滑动效果,结果报错闪退

java.lang.ArithmeticException: divide by zero
        at com.zyyoona7.wheel.WheelView.getCurrentPosition(WheelView.java:1177)
        at com.zyyoona7.wheel.WheelView.observeItemChanged(WheelView.java:1055)
        at com.zyyoona7.wheel.WheelView.invalidateIfYChanged(WheelView.java:1044)
        at com.zyyoona7.wheel.WheelView.onTouchEvent(WheelView.java:948)
        at android.view.View.dispatchTouchEvent(View.java:11820)

测试代码:

val hourView: WheelView<String> = findViewById(R.id.hour)
hourView.data = arrayListOf("01", "02", "03", "04", "05", "06", "07", "08", "09", "10")
hourView.isCyclic = true
hourView.visibleItems = 5
<LinearLayout
            android:id="@+id/timepicker"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:background="@android:color/white"
            android:orientation="horizontal"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent">

        <com.zyyoona7.wheel.WheelView
                android:id="@+id/hour"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="1"/>

        <com.zyyoona7.wheel.WheelView
                android:id="@+id/min"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="1"/>
    </LinearLayout>

报错位置应该是在com.zyyoona7.wheel.WheelView的getCurrentPosition()方法

    /**
     * 根据偏移计算当前位置下标
     *
     * @return 偏移量对应的当前下标
     */
    private int getCurrentPosition() {
        int itemPosition;
        if (mScrollOffsetY < 0) {
            itemPosition = (mScrollOffsetY - mItemHeight / 2) / mItemHeight;
        } else {
            itemPosition = (mScrollOffsetY + mItemHeight / 2) / mItemHeight;
        }
        int currentPosition = itemPosition % mDataList.size(); //此处报错了
        if (currentPosition < 0) {
            currentPosition += mDataList.size();
        }

        return currentPosition;
    }
zyyoona7 commented 5 years ago

非常详细的信息,感谢反馈,我晚上修复一下~

bkleopold commented 5 years ago

可能要补充下说明,就是随意滑动之后会偶尔出现这个问题,你可能要多试几次

zyyoona7 commented 5 years ago

好的,上面的测试代码是全部了吗?

zyyoona7 commented 5 years ago

是不是有一个 wheelView 没有设置 data 然后你滑动了它?

bkleopold commented 5 years ago

嗯,应该是我没设置好第二个wheelView的数据,抱歉。把这个问题关闭掉吧。

zyyoona7 commented 5 years ago

我记录一下,在 onTouchEvent 处做下限制