Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AndroidX首选项问题中的多屏幕_Android_Kotlin_Android Preferences_Androidx_Android Jetpack - Fatal编程技术网

AndroidX首选项问题中的多屏幕

AndroidX首选项问题中的多屏幕,android,kotlin,android-preferences,androidx,android-jetpack,Android,Kotlin,Android Preferences,Androidx,Android Jetpack,我的目标是使用AndroidX首选项构建一个多首选项屏幕,由主首选项root.xml和子首选项adv_preferences.xml组成。到目前为止,该应用程序只包含一个main活动和一些片段,如主屏幕片段、MyRootFragment和AdvPreferencesFragment MainActivity不包含任何与首选项相关的代码 MySettingsFragment.kt: class MyRootFragment : PreferenceFragmentCompat() { // ba

我的目标是使用AndroidX首选项构建一个多首选项屏幕,由主首选项root.xml和子首选项adv_preferences.xml组成。到目前为止,该应用程序只包含一个main活动和一些片段,如主屏幕片段、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)
    }
}
<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" />
root.xml(基本首选项):

到MainActivity中的onCreate()方法(如Android Jetpack首选项指南中),但这会导致以下崩溃和消息(应用程序甚至不会再启动):

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

尝试在mobile_navigation.xml中使用replace
像这样:

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

<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" />
supportFragmentManager
    .beginTransaction()
    .replace(R.id.settings, MyRootFragment())
    .commit()
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>