我正在使用navigation組件并bottomNavigationView一起使用。當我多次來回切換選項卡時,我收到崩潰訊息:
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:5361)
at android.view.ViewGroup.addView(ViewGroup.java:5190)
at android.view.ViewGroup.addView(ViewGroup.java:5162)
at androidx.appcompat.widget.Toolbar.addSystemView(Toolbar.java:1528)
at androidx.appcompat.widget.Toolbar.setTitle(Toolbar.java:777)
at com.atp.newarchitecture.activity.AppActivity.onCreate$lambda-0(AppActivity.kt:119)
代碼是:
navController.addOnDestinationChangedListener { _,
destination,
argument ->
binding.toolbar.title = resources.getString(when (destination.id) {
R.id.fragmentId -> R.string.fragmentTitle
//... more ids
})
布局檔案是:
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ToolBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="?attr/homeAsUpIndicator" />
<TextView
android:id="@ id/selected_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/login_text_input_layout"
app:layout_constraintBottom_toBottomOf="@ id/toolbar"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
tools:text="@string/selected_count" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/alert_tip_root"
android:layout_width="0dp"
android:layout_height="@dimen/quadruple_margin"
android:layout_margin="@dimen/default_margin"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/bottom_navigation_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:theme="@style/BottomNavigationTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_bar" />
<androidx.fragment.app.FragmentContainerView
android:id="@ id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/nav_graph" />
<View
android:id="@ id/loading_background_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
我知道崩潰訊息意味著一個視圖應該只有一個父視圖,但addView在這里似乎沒有。想知道是否每次都binding.root.title分配電話addView?有什么幫助嗎?謝謝!
編輯:
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
androidTestImplementation("androidx.navigation:navigation-testing:$nav_version")
implementation("androidx.navigation:navigation-compose:2.4.0-beta02")
uj5u.com熱心網友回復:
如導航 UI 指南中所述:
注意:如果您將 a
Toolbar作為引數傳遞給setSupportActionBar(),則ActionBar假定您完全擁有它,Toolbar并且在該呼叫之后您不得使用任何工具列 API。
在您呼叫 的每種情況下都是如此setSupportActionBar()。這意味著直接呼叫任何ToolbarAPI,例如setTitle,都會破壞所有權契約并導致此類例外。
相反,您應該:
根本不要用
setSupportActionBar()。您可以將工具列直接連接到導航并升級到AppCompat 1.4.0,以便片段通過MenuHost和MenuProviderAPI將元素添加到工具列。如果使用
setSupportActionBar(),則需要通過 ActionBar API 設定標題。即,呼叫supportActionBar!!.title = ...按照應用欄指南并
android:label在導航圖 XML 檔案(即android:label="@string/fragmentTitle")中的每個目的地設定。這將在當前目的地更改時自動更新標題(假設您已呼叫 的適當setup方法NavigationUI)。這將完全不需要使用您的偵聽器,從而允許您完全洗掉所有代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381260.html
