KingJA/LoadSir

onCreateView返回0时setCallBack出错

Pacey6 opened this issue · 5 comments

若仿照自带HintCallback那样在onCreateView中返回0,则:
loadService.setCallBack(HintCallback.class, (context, view1) -> {});
报错找不到资源0x00

@Pacey6 你好,可能注册时机不对,请参照Demo

teaim commented

@KingJA 测试发现, 必须先执行showCallback, 再执行setCallback才不会报"onCreateView中返回0"的异常

teaim commented

因为不执行show的, HintCallback的View不会添加上去, 此时的rootView为null, 默认会走inflate通过onCreateView获取布局id进行填充, 但是HintCallback不是通过布局生成View, 而是通过代码创建的View, 所以一定会报报"onCreateView中返回0"的异常

teaim commented

感觉可以和getRootView一样

    public View getRootView() {
        int resId = onCreateView();
        if (resId == 0 && rootView != null) {
            return rootView;
        }

        if (onBuildView(context) != null) {
            rootView = onBuildView(context);
        }

        if (rootView == null) {
            rootView = View.inflate(context, onCreateView(), null);
        }
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onReloadEvent(context, rootView)) {
                    return;
                }
                if (onReloadListener != null) {
                    onReloadListener.onReload(v);
                }
            }
        });
        onViewCreate(context, rootView);
        return rootView;
    }