每次切换到偏好设置活动时,都会遇到运行时错误,即我的应用程序。我正在尝试学习Android编程,我设法解决了我遇到的问题,但经过大量的时间寻找帮助后,我无法理解我做错了什么。
这是Settings.java的代码:
package com.wisp.sudokuapp;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Settings extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
我的settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckboxPreference
android:key="hints"
android:title="@string/settings_hints_title"
android:summary="@string/settings_hints_summary"
android:defaultValue="true" />
<CheckboxPreference
android:key="music"
android:title="@string/settings_music_title"
android:summary="@string/settings_music_summary"
android:defaultValue="true" />
</PreferenceScreen>
我的清单:
(...)
<activity android:name="com.wisp.sudokuapp.Settings"
android:label="@string/settings_title">
</activity>
(...)
和Logcat:
03-25 17:22:22.637: W/dalvikvm(440): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-25 17:22:22.687: E/AndroidRuntime(440): FATAL EXCEPTION: main
03-25 17:22:22.687: E/AndroidRuntime(440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wisp.sudokuapp/com.wisp.sudokuapp.Settings}: android.view.InflateException: Binary XML file line #5: Error inflating class CheckboxPreference
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.os.Looper.loop(Looper.java:123)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-25 17:22:22.687: E/AndroidRuntime(440): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 17:22:22.687: E/AndroidRuntime(440): at java.lang.reflect.Method.invoke(Method.java:507)
03-25 17:22:22.687: E/AndroidRuntime(440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-25 17:22:22.687: E/AndroidRuntime(440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-25 17:22:22.687: E/AndroidRuntime(440): at dalvik.system.NativeStart.main(Native Method)
03-25 17:22:22.687: E/AndroidRuntime(440): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class CheckboxPreference
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
03-25 17:22:22.687: E/AndroidRuntime(440): at com.wisp.sudokuapp.Settings.onCreate(Settings.java:11)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-25 17:22:22.687: E/AndroidRuntime(440): ... 11 more
03-25 17:22:22.687: E/AndroidRuntime(440): Caused by: java.lang.ClassNotFoundException: android.preference.CheckboxPreference in loader dalvik.system.PathClassLoader[/data/app/com.wisp.sudokuapp-1.apk]
03-25 17:22:22.687: E/AndroidRuntime(440): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-25 17:22:22.687: E/AndroidRuntime(440): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-25 17:22:22.687: E/AndroidRuntime(440): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.createItem(GenericInflater.java:375)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
03-25 17:22:22.687: E/AndroidRuntime(440): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
03-25 17:22:22.687: E/AndroidRuntime(440): ... 19 more
我知道该方法已过时,但我希望它能与2.3.x一起使用。为了学习android开发我正在读一本书,并且在书中似乎有用。我发现注释掉addPreferencesFromResource行会使应用程序崩溃,但显然不会加载任何UI。
我真的很感激你的帮助。
抱歉我的英文。
答案 0 :(得分:5)
问题在于xml文件中的拼写错误。将CheckboxPreference更改为CheckBoxPreference。
答案 1 :(得分:0)
你有拼写错误。二,事实上。尝试:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="hints"
android:title="@string/settings_hints_title"
android:summary="@string/settings_hints_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="music"
android:title="@string/settings_music_title"
android:summary="@string/settings_music_summary"
android:defaultValue="true" />
</PreferenceScreen>
Android XML区分大小写,因此CheckboxPreference
与CheckBoxPreference
不同,不存在。