未找到偏好“屏幕”的依赖性“开关”
当我使用ListPreference时,一切都可以在android:dependency =“ switch”上正常工作,而且当我关闭android:dependency =“ switch”时-我可以打开PreferenceScreen。
但是要解锁SwitchPreference并打开PreferenceScreen,则会出现错误。
java.lang.IllegalStateException:找不到首选项“屏幕”的依赖项“开关”(标题:“屏幕”
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:title="Switch"
android:key="switch"
android:summaryOn="Description of switch 2 on"
android:summaryOff="Description of switch 2 off"/>
<PreferenceScreen
android:dependency="switch"
android:key="screen"
android:summaryOn="Description of screen on"
android:summaryOff="Description of screen off"
android:title="Screen">
<CheckBoxPreference
android:summaryOn="Description of checkbox on"
android:summaryOff="Description of checkbox off"
android:title="CheckBox"
android:key="checkbox"/>
</PreferenceScreen>
</PreferenceScreen>
答案 0 :(得分:0)
我以编程方式解决了此问题,但只有xml无法正常工作
class SettingsFragment : PreferenceFragmentCompat(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.ex_003_preference, rootKey)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val switch = preferenceScreen.findPreference("switch") as SwitchPreference
val screen = preferenceManager.findPreference("screen") as PreferenceScreen
screen.isEnabled = false
switch.isChecked = false
switch.setOnPreferenceClickListener {
screen.isEnabled = switch.isChecked
return@setOnPreferenceClickListener false
}
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onPreferenceStartScreen(preferenceFragmentCompat: PreferenceFragmentCompat?, preferenceScreen: PreferenceScreen?): Boolean {
preferenceFragmentCompat?.preferenceScreen = preferenceScreen
return true
}
override fun getCallbackFragment(): Fragment {
return this
}
}