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

朴灿烈づ我的快乐病毒、 2022-12-28 09:16 163阅读 0赞

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

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

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

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

发表评论

表情:
评论列表 (有 0 条评论,163人围观)

还没有评论,来说两句吧...

相关阅读