每次我切换到偏好活动,我得到一个运行时错误,fc我的应用程序。我试图学习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。
我真的很感谢你的帮助。抱歉我的英语不好
问题是xml文件中的一个错别字。将CheckboxPreference改为CheckboxPreference
你有一个拼写错误。两个,事实上。试一试:
<?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
是不同的,不存在的东西。