java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用removeView()

时间:2019-10-26 09:45:31

标签: java android kotlin

用Kotlin语言为Android制作小型Tasker,我遇到了导航问题。 当我在主屏幕(ListFragment)上按“添加”按钮时,我希望我的应用程序导航到另一个屏幕(CreateTaskFragment),用户可以在其中创建任务,但是android studio却给了我这样的堆栈跟踪信息:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.oleksii.routinetracker, PID: 21231
    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:5034)
        at android.view.ViewGroup.addView(ViewGroup.java:4865)
        at android.view.ViewGroup.addView(ViewGroup.java:4805)
        at android.view.ViewGroup.addView(ViewGroup.java:4778)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我该怎么做才能解决此问题?这是我的代码:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

class ListFragment : Fragment() {

    private lateinit var viewModel: ListViewModel
    private lateinit var binding: FragmentListBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?): View? {

        viewModel = ViewModelProviders.of(this).get(ListViewModel::class.java)
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_list, container, false)

        binding.addButton.setOnClickListener {
            findNavController().navigate(R.id.action_listFragment_to_createTaskFragment)
        }

        return binding.root
    }
}
class CreateTaskFragment : Fragment() {

    private lateinit var viewModel: CreateTaskViewModel
    private lateinit var binding: FragmentCreateTaskBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?): View? {

        viewModel = ViewModelProviders.of(this).get(CreateTaskViewModel::class.java)
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_create_task, container, true)

        binding.button.setOnClickListener {view: View ->
            view.findNavController().navigate(R.id.action_createTaskFragment_to_listFragment)
        }

        return binding.root
    }


}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="com.oleksii.routinetracker.list.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list" >
        <action
            android:id="@+id/action_listFragment_to_createTaskFragment"
            app:destination="@id/createTaskFragment"
            app:launchSingleTop="true"/>
    </fragment>
    <fragment
        android:id="@+id/createTaskFragment"
        android:name="com.oleksii.routinetracker.createtask.CreateTaskFragment"
        android:label="fragment_create_task"
        tools:layout="@layout/fragment_create_task">
        <action
            android:id="@+id/action_createTaskFragment_to_listFragment"
            app:destination="@id/listFragment" />
    </fragment>
</navigation>

还有一些xml文件:

<!-- fragment_list -->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.oleksii.routinetracker.list.ListFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/list_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/tasks_scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <LinearLayout
                android:id="@+id/tasks_lin"
                style="@style/tasks"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingTop="@dimen/small_padding"
                    android:fontFamily="@font/roboto"
                    android:text="@string/my_tasks"
                    android:textColor="@color/colorBlack"
                    android:textSize="48sp" />
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_end="96dp" />

        <Button
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add"
            android:textColor="@color/colorBlue500"
            android:textSize="18sp"
            android:background="@drawable/custom_button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<!-- activity_main -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/myNavHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/navigation"
        app:defaultNavHost="true" />
</androidx.constraintlayout.widget.ConstraintLayout>

提前谢谢!

1 个答案:

答案 0 :(得分:1)

binding = DataBindingUtil.inflate(inflater, R.layout.fragment_create_task, container, true)

在此处将最后一个true更改为false。您不应在此处将膨胀视图添加到容器中;让片段管理器为您完成。