2014-10-09 92 views
0

我嘗試在我的應用程序中設置默認值。Android應用程序通過設置默認值崩潰

我嘗試這樣做:

PreferenceManager.setDefaultValues(this, R.xml.defaults, false); 

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); 
PreferenceManager.setDefaultValues(this, R.xml.defaults, false); 

但它總是崩潰。

這裏我RES/XML/defaults.xml中

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="programTitles">["x", "a", "b", "c", "d", "e"]</string> 
    <string name="programSubs">["", "as", "bs", "cs", "ds", "es"]</string> 
</resources> 

的logcat:

10-09 08:16:21.820: W/dalvikvm(1557): threadid=1: thread exiting with uncaught exception (group=0xb1a5fba8) 
10-09 08:16:21.840: E/AndroidRuntime(1557): FATAL EXCEPTION: main 
10-09 08:16:21.840: E/AndroidRuntime(1557): Process: com.example.blauzahn, PID: 1557 
10-09 08:16:21.840: E/AndroidRuntime(1557): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blauzahn/com.example.blauzahn.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class resources 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.os.Handler.dispatchMessage(Handler.java:102) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.os.Looper.loop(Looper.java:136) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at java.lang.reflect.Method.invoke(Method.java:515) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at dalvik.system.NativeStart.main(Native Method) 
10-09 08:16:21.840: E/AndroidRuntime(1557): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class resources 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.GenericInflater.inflate(GenericInflater.java:317) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.GenericInflater.inflate(GenericInflater.java:263) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:485) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at com.example.blauzahn.MainActivity.onCreate(MainActivity.java:79) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.Activity.performCreate(Activity.java:5231) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  ... 11 more 
10-09 08:16:21.840: E/AndroidRuntime(1557): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[zip file "/data/app/com.example.blauzahn-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.blauzahn-1, /system/lib]] 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at java.lang.ClassLoader.loadClass(ClassLoader.java:497) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at java.lang.ClassLoader.loadClass(ClassLoader.java:457) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.GenericInflater.createItem(GenericInflater.java:375) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428) 
10-09 08:16:21.840: E/AndroidRuntime(1557):  ... 20 more 

對我來說,這似乎是 「資源」 是一個錯誤的標籤或類似的東西。這是嗎?我需要哪個標籤?

+2

哪裏是你的logcat然後???? – 2014-10-09 10:57:53

+0

字符串資源是一個數組? – RvdK 2014-10-09 10:59:45

+0

' [「x」,「a」,「b」,「c」,「d」,「e」]這是**不是**字符串數組。 – 2014-10-09 11:01:17

回答

0

從資源刪除<resources>標籤/ XML/defaults.xml中

該文件應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 

<PreferenceCategory 
    android:title="PreferenceCategory A"> 

    <CheckBoxPreference 
     android:key="checkbox_preference" 
     android:title="title_checkbox_preference" 
     android:summary="summary_checkbox_preference" /> 

</PreferenceCategory> 

<PreferenceCategory 
    android:title="PreferenceCategory B"> 

    <EditTextPreference 
     android:key="edittext_preference" 
     android:title="title_edittext_preference" 
     android:summary="summary_edittext_preference" 
     android:dialogTitle="dialog_title_edittext_preference" /> 

</PreferenceCategory>