AndroidX首选项问题中的多屏幕

问题描述 投票:0回答:1

我的目标是使用AndroidX首选项构建一个包含主要首选项root.xml和子首选项adv_preferences.xml的多重首选项屏幕。到目前为止,该应用程序仅包含MainActivity和一些片段,例如主屏幕片段MyRootFragment和AdvPreferencesFragment。

Settings Screenshot

MainActivity不包含任何与首选项相关的代码。

MySettingsFragment.kt

class MyRootFragment : PreferenceFragmentCompat() { // basic preferences
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root, rootKey)
    }
}
class AdvPreferencesFragment : PreferenceFragmentCompat() { // advanced preferences
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.adv_preferences, rootKey)
    }
}

root.xml(基本首选项):

<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/settings">
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="display_text"
        android:summaryOff="Invisible"
        android:summaryOn="Visible"
        android:title="blabla"/>
    <Preference
        app:title="Advanced Settings"
        app:summary="Test for using multiple screens"
        app:fragment="com.example.app.ui.settings.AdvPreferencesFragment"/>
</androidx.preference.PreferenceScreen>

adv_preferences.xml(高级首选项):

<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Preference
        android:key="preference_key"
        android:title="placeholder"
        android:summary="placeholder" />
</androidx.preference.PreferenceScreen>

mobile_navigation.xml

<fragment
    android:id="@+id/nav_root_preferences"
    android:name="com.example.app.ui.settings.MyRootFragment"
    android:label="@string/menu_preferences"
    tools:layout="@xml/root" />
<fragment
    android:id="@+id/nav_adv_preferences"
    android:name="com.example.app.ui.settings.AdvPreferencesFragment"
    android:label="@string/menu_preferences"
    tools:layout="@xml/adv_preferences" />

每次我点击高级设置时,应用程序都会崩溃并显示以下日志消息:

java.lang.IllegalStateException:片段AdvPreferencesFragment {...}声明了目标片段不属于此FragmentManager的MySettingsFragment {...}!

我该如何解决?片段,xml文件和导航图中的所有内容似乎都已正确连接。

我已经尝试添加以下内容

supportFragmentManager
    .beginTransaction()
    .replace(R.id.settings, MyRootFragment())
    .commit()

进入MainActivity中的onCreate()方法(如Android Jetpack偏好指南中所述,但这会导致以下崩溃和消息(该应用程序甚至无法启动):

java.lang.IllegalArgumentException:找不到ID的视图…(com.example.app:id/settings)的片段MySettingsFragment {...}

android kotlin android-preferences androidx android-jetpack
1个回答
0
投票
try use <preference> replace <fragment> in mobile_navigation.xml

像这样:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Preference
        app:fragment="com.xxx.mypreference.AdvPreferencesFragment"
        app:key="notification_category"
        app:title="Adv"
        app:summary="Adv">
    </Preference>
</PreferenceScreen>
© www.soinside.com 2019 - 2024. All rights reserved.