lcodecorex / TwinklingRefreshLayout

RefreshLayout that support for OverScroll and better than iOS. 支持下拉刷新和上拉加载的RefreshLayout,自带越界回弹效果,支持RecyclerView,AbsListView,ScrollView,WebView
Apache License 2.0
4k stars 672 forks source link

mHeadLayout.removeAllViewsInLayout(); --> null #48

Closed mecobean closed 7 years ago

mecobean commented 7 years ago
错误日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.FrameLayout.removeAllViewsInLayout()' on a null object reference
    at com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout$1.run(TwinklingRefreshLayout.java:219)
    at android.os.Handler.handleCallback(Handler.java:743)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:5665)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)

框架源码:
 /**
     * 设置头部View
     */
    public void setHeaderView(final IHeaderView headerView) {
        if (headerView != null) {
            post(new Runnable() {
                @Override
                public void run() {
                    mHeadLayout.removeAllViewsInLayout();
                    mHeadLayout.addView(headerView.getView());
                }
            });
            mHeadView = headerView;
        }
    }
sisel commented 7 years ago

onAttachedToWindow 此前没有被回调

sisel commented 7 years ago

不解为何要在onAttachedToWindow中初始化:mHeadLayout,mExtraHeadLayout,mBottomLayout

看代码移到constructor也没问题吧

lcodecorex commented 7 years ago

@sisel 因为在构造函数中初始化mHeadLayout,mExtraHeadLayout,mBottomLayout,可滑动View最后被inflate进来=getChildAt(3)。而在onAttachedToWindow方法中可滑动View始终为getChildAt(0)。目前已修改。

sisel commented 7 years ago

已修改?好像没看到commit,应该怎么避免这个bug呢?

lcodecorex commented 7 years ago

@sisel 在构造函数中初始化mHeadLayout,mExtraHeadLayout,mBottomLayout,然后在onFinishInflate方法中获取到可滑动View:

@Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //获得子控件
        //TODO 可能引入新问题,fixedHeader显示异常
        mChildView = getChildAt(3);

        cp.init();
    }

没有提交是因为引入了一点小问题

sisel commented 7 years ago

是不是可以改为在构造方法中创建,在onFinishInflate中add 这样就不存在次序问题

lcodecorex commented 7 years ago

v1.06已修复,感谢反馈!