Android开发报错: The specified child already has a parent.You must call removeView()

出错位置:我的是Fragment类报的这个错:

 @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
    
        View view=inflater.inflate(R.layout.fragment_explore,container);
        return view;
    }

出错位置:
view=inflater.inflate(R.layout.fragment_explore,container);
改为:
view=inflater.inflate(R.layout.fragment_explore,null);
inflate()第一个参数是页面,第二个设置为空即可
正确姿势:

 @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
    
        View view=inflater.inflate(R.layout.fragment_explore,null);
        return view;
    }

猜你喜欢

转载自blog.csdn.net/ShiXinXin_Harbour/article/details/111265421
今日推荐