在片段中使用数据绑定时发生异常:“指定的子代已经有一个父代.您必须先在孩子的父级上调用removeView(). [英] Exception when using data binding in fragment: "The specified child already has a parent. You must call removeView() on the child's parent first"

查看:67
本文介绍了在片段中使用数据绑定时发生异常:“指定的子代已经有一个父代.您必须先在孩子的父级上调用removeView().的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio 3.1,java 1.8

Android Studio 3.1, java 1.8

我尝试使用数据绑定:

此处 settings.xml 布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">    
    <data>    
        <variable
            name="handler"
            type="com.myproject.SettingsFragment" />    
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/contentContainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">


                    <android.support.constraint.ConstraintLayout
                        android:id="@+id/contactUsContainer"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:onClick="@{handler::onClickContactUs}">

                        <ImageView
                            android:id="@+id/contactUsImageView"
                            android:layout_width="28dp"
                            android:layout_height="28dp"
                            app:srcCompat="@drawable/ic_settings_contacts_us" 

                        <TextView
                            android:id="@+id/contactUsTextView"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:text="@string/contact_us"/>

                    </android.support.constraint.ConstraintLayout>    

                </android.support.constraint.ConstraintLayout>
            </FrameLayout>
        </ScrollView>
    </android.support.constraint.ConstraintLayout>    
</layout>

此处片段 SettingsFragment.java :

public class SettingsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        SettingsBinding binding = DataBindingUtil.setContentView(getActivity(), R.layout.settings);
        binding.setHandler(this);
        return binding.getRoot();
    }

    public void onClickContactUs(View view) {
    }
}

但是我得到了错误:

    FATAL EXCEPTION: main
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
        at android.view.ViewGroup.addView(ViewGroup.java:3208)
        at android.view.ViewGroup.addView(ViewGroup.java:3165)
        at android.view.ViewGroup.addView(ViewGroup.java:3145)
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1425)
android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)

推荐答案

DataBindingUtil.setContentView()用于在Activity中进行绑定,因为该方法将设置Activity的内容视图(就像您通常对setContentView所做的那样) () 方法).这就是为什么您会看到view(binding.getRoot())已经附加到父视图的异常原因.

DataBindingUtil.setContentView() should be use for binding in an Activity because the method will set the content view for the Activity(like you would normally do with the setContentView() method). This is why you get the exception that the view(binding.getRoot()) is already appended to a parent view.

在您的片段中,使用DataBindingUtil.inflate()扩展布局并创建绑定,而无需实际附加视图(片段稍后将自己执行):

In your fragment use DataBindingUtil.inflate() to inflate the layout and create the binding without actually appending the view(which the fragment does itself later):

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    SettingsBinding binding = DataBindingUtil.inflate(inflater, R.layout.settings, container, false);
    binding.setHandler(this);
    return binding.getRoot();
}

这篇关于在片段中使用数据绑定时发生异常:“指定的子代已经有一个父代.您必须先在孩子的父级上调用removeView().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆