gpt4 book ai didi

android - 在 fragment : "The specified child already has a parent. You must call removeView() on the child' s parent first"中使用数据绑定(bind)时出现异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:28 31 4
gpt4 key购买 nike

安卓工作室 3.1,java 1.8

我尝试使用数据绑定(bind):

此处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>

这里是 fragment 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 中进行绑定(bind),因为该方法将为 Activity 设置内容 View (就像您通常使用 setContentView() 方法所做的那样)。这就是为什么您会收到异常,即 View (binding.getRoot()) 已附加到父 View 。

在您的 fragment 中使用 DataBindingUtil.inflate() 来扩充布局并创建绑定(bind),而无需实际附加 View ( fragment 稍后自行执行):

@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();
}

关于android - 在 fragment : "The specified child already has a parent. You must call removeView() on the child' s parent first"中使用数据绑定(bind)时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47844686/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com