4

我做了一個簡單的PreferenceScreen與多個Preference CategorysCheckBoxPreferences爲什麼不能用SwitchPreference交換CheckBoxPreference?

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 

    <PreferenceCategory android:title="General"> 

     <CheckBoxPreference 
      android:title="GPS" 
      android:key="gps_preference" 
      android:summary="Enales the GPS tracking of your device" 
      android:defaultValue="true" 
      ></CheckBoxPreference> 

     <SwitchPreference 
     android:title="WLAN" 
     android:key="wlan_preference" 
     android:summary="Enables the WLAN usage" 
     android:defaultValue="true"></SwitchPreference> 

     <CheckBoxPreference 
      android:title="Benachrichtigungen" 
      android:key="benachrichtigung_preference" 
      android:summary="Enables the Push messages" 
      android:defaultValue="true" 
      ></CheckBoxPreference> 

    </PreferenceCategory> 
</PreferenceScreen> 

此首選項屏幕在android.support.v7.preference.PreferenceFragmentCompat之內。如果我像這樣離開everyhing,那麼一切正常。

但是如果我想轉出去SwitchPreference一個CheckBoxPreference我得到一個渲染錯誤在我design標籤(應該通過簡單地切換出來的XML標記的CheckBoxPreferenceSwitchPreference工作):

在渲染過程中發生異常: android.preference.CheckBoxPreference不能轉換到 android.preference.GenericInflater $父

堆棧跟蹤:

android.view.InflateException: Binary XML file line #15: Error inflating class (not found)SwitchPreference 
                       at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:301) 
                       at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359) 
                       at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:361) 
                       at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167) 
                       at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117) 
                       at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:115) 
                       at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:424) 
                       at com.julianriegraf.app2night.SettingsFragment.onCreatePreferences(SettingsFragment.java:22) 
                       at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:216) 
                       at android.support.v4.app.Fragment.performCreate(Fragment.java:2062) 
                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051) 
                       at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:795) 
                       at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:835) 
                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:676) 
                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671) 
                       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                      Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.preference.SwitchPreference" on path: DexPathList[[zip file "/data/app/com.julianriegraf.app2night-2/base.apk"],nativeLibraryDirectories=[/data/app/com.julianriegraf.app2night-2/lib/x86, /vendor/lib, /system/lib]] 
                       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 
                       at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
                       at java.lang.ClassLoader.loadClass(ClassLoader.java:469) 
                       at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:233) 
                       at android.support.v7.preference.PreferenceInflater.onCreateItem(PreferenceInflater.java:281) 
                       at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:290) 
                       at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359)  
                       at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:361)  
                       at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)  
                       at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)  
                       at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:115)  
                       at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:424)  
                       at com.julianriegraf.app2night.SettingsFragment.onCreatePreferences(SettingsFragment.java:22)  
                       at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:216)  
                       at android.support.v4.app.Fragment.performCreate(Fragment.java:2062)  
                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)  
                       at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:795)  
                       at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:835)  
                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:676)  
                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)  
                       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)  
                       at android.os.Handler.handleCallback(Handler.java:739)  
                       at android.os.Handler.dispatchMessage(Handler.java:95)  
                       at android.os.Looper.loop(Looper.java:148)  
                       at android.app.ActivityThread.main(ActivityThread.java:5417)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

如果修改後的XML在您運行應用程序時起作用,並且只有設計選項卡抱怨,那麼很可能這是Android Studio中的一個錯誤。 – CommonsWare

+0

是的,但它不是在我的設備上工作,以及... – user343037

+0

你應該在那裏得到一些其他的錯誤。 [檢查LogCat](http://stackoverflow.com/questions/23353173/unwanted-myapp-has-stopped-how-can-i-solve-this)看看你是如何崩潰。 – CommonsWare

回答

5

沒有類android.support.v7.preference.SwitchPreference。有android.support.v7.preference.SwitchPreferenceCompat,android.support.v14.preference.SwitchPreference和原生android.preference.SwitchPreference

+0

非常感謝,有點錯過了一個:) – user343037