首选项支持库 - SwitchPreference 不工作
Preferences Support Library - SwitchPreference not working
几天前Google介绍了Preference Support Library
(Link)。我刚刚尝试在我的应用程序中实现它,尽管它似乎无法与 SwitchPreferences
一起使用,这很奇怪,因为 Google 声明我们可以像以前一样使用相同的 XML-Files 和明确表示 SwitchPreferences 现在可用于所有 API 7+ 设备。
引用 (Source)
[...] and add preferences using the same preference XML files
(http://goo.gl/wOcIxI), while adding support for elements such as
SwitchPreference (previously only available on API 14+ devices) to all
API 7+ devices. [...]
错误信息
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package/com.my.package.Main}: android.view.InflateException: Binary XML file line #4: Error inflating class (not found)SwitchPreference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
相关部分显然是Binary XML file line #4: Error inflating class (not found)SwitchPreference
。
Java代码
public class FragmentSettings extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.preferences);
}
}
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:key="pref_something"
android:summary="Lorem ipsum dolor sit amet"
android:title="Lorem ipsum" />
</PreferenceScreen>
所以基本上问题是:我做错了什么或者这确实是首选项支持库的错误?
SwitchPreference
添加到 API 级别 14。如果您使用的是首选项支持库 v7,则必须改用 SwitchPreferenceCompat
。
<SwitchPreferenceCompat
android:key="pref_something"
android:summary="Lorem ipsum dolor sit amet"
android:title="Lorem ipsum" />
几天前Google介绍了Preference Support Library
(Link)。我刚刚尝试在我的应用程序中实现它,尽管它似乎无法与 SwitchPreferences
一起使用,这很奇怪,因为 Google 声明我们可以像以前一样使用相同的 XML-Files 和明确表示 SwitchPreferences 现在可用于所有 API 7+ 设备。
引用 (Source)
[...] and add preferences using the same preference XML files (http://goo.gl/wOcIxI), while adding support for elements such as SwitchPreference (previously only available on API 14+ devices) to all API 7+ devices. [...]
错误信息
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package/com.my.package.Main}: android.view.InflateException: Binary XML file line #4: Error inflating class (not found)SwitchPreference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
相关部分显然是Binary XML file line #4: Error inflating class (not found)SwitchPreference
。
Java代码
public class FragmentSettings extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.preferences);
}
}
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:key="pref_something"
android:summary="Lorem ipsum dolor sit amet"
android:title="Lorem ipsum" />
</PreferenceScreen>
所以基本上问题是:我做错了什么或者这确实是首选项支持库的错误?
SwitchPreference
添加到 API 级别 14。如果您使用的是首选项支持库 v7,则必须改用 SwitchPreferenceCompat
。
<SwitchPreferenceCompat
android:key="pref_something"
android:summary="Lorem ipsum dolor sit amet"
android:title="Lorem ipsum" />