出现原因:在fragment中动态添加一个layout

例如:

View notDataView = getLayoutInflater().inflate(R.layout.empty_view, recyclerView, false);

解决方案:

更换为:

View notDataView = LayoutInflater.from(getActivity()).inflate(R.layout.empty_view, recyclerView, false);

总结:在activity中使用

getLayoutInflater().inflate

在fragment中使用

LayoutInflater.from(getActivity()).inflate

注意:在fragment中使用LayoutInflater.from(getActivity()).inflate时要记得判断getActivity()是否为空,否则在某些特殊情景下会崩溃噢(例如,在加载动态layout时退出了此fragment)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐