<Android 异常>Framgment常见异常

    xiaoxiao2025-11-18  1

    <pre class="java" name="code"> public class contentFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.content_layout,container); return view; } }

    然后我在别的类中

    contentFragment contentFragment1 = new contentFragment();完成各种代码后就报以下的错误了

    Caused by:java.lang.IllegalStateException:

    The specified child already has a parent. You must call removeView() on the child's parent first.

    异常说明:所用的子类已经在一个父类中View,必须在第一个父类移除显示(removeView)

    也即是说你的view已经显示在一个view,如果你要显示在另一个view中,请在第一个view中removeView后才能显示其他view中

    第一种解决方法:

    把代码改成以下就能显示

    public class contentFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.content_layout,container,false); return view; } }

    第二中解决方法:

    在父类view中调用removView();

    转载请注明原文地址: https://ju.6miu.com/read-1304310.html
    最新回复(0)