每次我切换到首选项活动时,我都会收到一个运行时错误,该错误会影响我的应用程序。我正在尝试学习 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);
}
}
我的设置.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。
我真的很感激你的帮助。
对不起我的英语不好。