1. 程式人生 > >java.lang.IllegalStateException: onMeasure() did not set the measured dimension by calling setMeasur

java.lang.IllegalStateException: onMeasure() did not set the measured dimension by calling setMeasur

今天自定義Recycleview時,呼叫

adapter.notifyDataSetChanged();

報異常,異常資訊如下:

 java.lang.IllegalStateException: View with id 2131230785: com.example.smartcity.widget.viewpageRecycleview.PageRecyclerView#onMeasure() did not set the measured dimension by calling setMeasuredDimension()

解決方法:在onMeasure()裡新增:setMeasuredDimension()

 @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        setMeasuredDimension(widthSpec, heightSpec);
        super.onMeasure(widthSpec, heightSpec);
        shortestDistance = getMeasuredWidth() / 3;
    }