【问题标题】:Multiple Screen in AndroidX Preferences issueAndroidX 首选项中的多个屏幕问题
【发布时间】:2020-03-07 19:18:28
【问题描述】:

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

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" />

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

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

我该如何解决这个问题?片段、xml 文件和导航图中的一切似乎都正确连接。

我已经尝试添加以下内容

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

转到 MainActivity 中的 onCreate() 方法(如 Android Jetpack Preference Guide 中所述),但这会导致以下崩溃和消息(应用程序甚至不再启动):

java.lang.IllegalArgumentException: No view found for id … (com.example.app:id/settings) 用于片段 MySettingsFragment{...}

【问题讨论】:

标签: android kotlin android-preferences androidx android-jetpack


【解决方案1】:
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>

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 2013-08-10
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2016-05-16
    • 2014-03-16
    相关资源
    最近更新 更多