为什么我的 PreferenceScreen 会导致强制关闭?
我正在让我的偏好看起来更好一点。我使用 IconPreferenceScreen 类,但是当我将所有 PreferenceScreen 更改为 IconPreferenceScreen 时,有些会强制关闭。
我展示的第一个偏好工作完美,但第二个偏好关闭。
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:icon="http://schemas.android.com/apk/res/com.andrew.notifyme"
android:key="preference_screen"
android:title="@string/app_name_formatted">
<PreferenceCategory
android:key="test_app_category"
android:title="@string/test_text">
<com.andrew.notifyme.preferences.IconPreferenceScreen
android:key="test_app"
android:title="@string/test_text"
icon:icon="@drawable/ic_test"
android:dependency="app_enabled" >
</com.andrew.notifyme.preferences.IconPreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:key="basic_settings_category"
android:title="@string/basic_settings_text">
<com.andrew.notifyme.preferences.IconPreferenceScreen
android:key="basic_settings_screen"
android:title="Themes"
icon:icon="@drawable/ic_test"
android:dependency="app_enabled" >
<PreferenceScreen
android:key="quick_reply_settings_screen"
android:title="@string/quick_reply_settings_text"
android:dependency="app_enabled">
<CheckBoxPreference
android:key="quick_reply_save_draft_enabled"
android:title="@string/preference_save_draft_enabled_title"
android:summaryOn="@string/preference_save_draft_enabled_summaryOn"
android:summaryOff="@string/preference_save_draft_enabled_summaryOff"
android:defaultValue="true" />
</com.andrew.notifyme.preferences.IconPreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
堆栈跟踪:
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): FATAL EXCEPTION: main
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andrew.notifyme/com.andrew.notifyme.preferences.MainPreferenceActivity}: java.lang.ClassCastException: com.andrew.notifyme.preferences.IconPreferenceScreen
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.os.Looper.loop(Looper.java:130)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.main(ActivityThread.java:3835)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at dalvik.system.NativeStart.main(Native Method)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): Caused by: java.lang.ClassCastException: apps.droidnotify.preferences.IconPreferenceScreen
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at com.andrew.notifyme.preferences.MainPreferenceActivity.onCreate(MainPreferenceActivity.java:103)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): ... 11 more
抛出异常的代码:
public class MainPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
//Google Market URL
private static final String RATE_APP_ANDROID_URL = "http://market.android.com/details?id=com.andrew.notifyme";
//Amazon Appstore URL
private static final String RATE_APP_AMAZON_URL = "http://www.amazon.com/gp/mas/dl/android?p=com.andrew.notifyme";
private static final String APP_ENABLED_KEY = "app_enabled";
private static final String CALENDAR_NOTIFICATIONS_ENABLED_KEY = "calendar_notifications_enabled";
private static final String LANDSCAPE_SCREEN_ENABLED_KEY = "landscape_screen_enabled";
private static final String CALENDAR_SELECTION_KEY = "calendar_selection";
private static final String CALENDAR_POLLING_FREQUENCY_KEY = "calendar_polling_frequency";
private static final int NOTIFICATION_TYPE_TEST = -1;
private boolean _debug = false;
private Context _context = null;
private boolean _debugCalendar = false;
private SharedPreferences _preferences = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_debug = Log.getDebug();
if (_debug) Log.v("MainPreferenceActivity.onCreate()");
_debugCalendar = Log.getDebugCalendar();
_context = MainPreferenceActivity.this;
_preferences = PreferenceManager.getDefaultSharedPreferences(_context);
_preferences.registerOnSharedPreferenceChangeListener(this);
//Don't rotate the Activity when the screen rotates based on the user preferences.
if(!_preferences.getBoolean(LANDSCAPE_SCREEN_ENABLED_KEY, false)){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
addPreferencesFromResource(R.xml.preferences);
setupCustomPreferences();
runOnceAlarmManager();
setupAppDebugMode(_debug);
setupRateAppPreference();
setupImportPreferences();
runOnceEula();
}
I'm in the process of making my preferences look a little nicer. I'm adding icons to each of them using the IconPreferenceScreen class, but as I change all my PreferenceScreens to IconPreferenceScreen some give me a force close.
The first preference I show works perfectly, but the second one force closes.
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:icon="http://schemas.android.com/apk/res/com.andrew.notifyme"
android:key="preference_screen"
android:title="@string/app_name_formatted">
<PreferenceCategory
android:key="test_app_category"
android:title="@string/test_text">
<com.andrew.notifyme.preferences.IconPreferenceScreen
android:key="test_app"
android:title="@string/test_text"
icon:icon="@drawable/ic_test"
android:dependency="app_enabled" >
</com.andrew.notifyme.preferences.IconPreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:key="basic_settings_category"
android:title="@string/basic_settings_text">
<com.andrew.notifyme.preferences.IconPreferenceScreen
android:key="basic_settings_screen"
android:title="Themes"
icon:icon="@drawable/ic_test"
android:dependency="app_enabled" >
<PreferenceScreen
android:key="quick_reply_settings_screen"
android:title="@string/quick_reply_settings_text"
android:dependency="app_enabled">
<CheckBoxPreference
android:key="quick_reply_save_draft_enabled"
android:title="@string/preference_save_draft_enabled_title"
android:summaryOn="@string/preference_save_draft_enabled_summaryOn"
android:summaryOff="@string/preference_save_draft_enabled_summaryOff"
android:defaultValue="true" />
</com.andrew.notifyme.preferences.IconPreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
Stack trace:
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): FATAL EXCEPTION: main
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andrew.notifyme/com.andrew.notifyme.preferences.MainPreferenceActivity}: java.lang.ClassCastException: com.andrew.notifyme.preferences.IconPreferenceScreen
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.os.Looper.loop(Looper.java:130)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.main(ActivityThread.java:3835)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at dalvik.system.NativeStart.main(Native Method)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): Caused by: java.lang.ClassCastException: apps.droidnotify.preferences.IconPreferenceScreen
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at com.andrew.notifyme.preferences.MainPreferenceActivity.onCreate(MainPreferenceActivity.java:103)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): ... 11 more
Code where exception is thrown:
public class MainPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
//Google Market URL
private static final String RATE_APP_ANDROID_URL = "http://market.android.com/details?id=com.andrew.notifyme";
//Amazon Appstore URL
private static final String RATE_APP_AMAZON_URL = "http://www.amazon.com/gp/mas/dl/android?p=com.andrew.notifyme";
private static final String APP_ENABLED_KEY = "app_enabled";
private static final String CALENDAR_NOTIFICATIONS_ENABLED_KEY = "calendar_notifications_enabled";
private static final String LANDSCAPE_SCREEN_ENABLED_KEY = "landscape_screen_enabled";
private static final String CALENDAR_SELECTION_KEY = "calendar_selection";
private static final String CALENDAR_POLLING_FREQUENCY_KEY = "calendar_polling_frequency";
private static final int NOTIFICATION_TYPE_TEST = -1;
private boolean _debug = false;
private Context _context = null;
private boolean _debugCalendar = false;
private SharedPreferences _preferences = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_debug = Log.getDebug();
if (_debug) Log.v("MainPreferenceActivity.onCreate()");
_debugCalendar = Log.getDebugCalendar();
_context = MainPreferenceActivity.this;
_preferences = PreferenceManager.getDefaultSharedPreferences(_context);
_preferences.registerOnSharedPreferenceChangeListener(this);
//Don't rotate the Activity when the screen rotates based on the user preferences.
if(!_preferences.getBoolean(LANDSCAPE_SCREEN_ENABLED_KEY, false)){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
addPreferencesFromResource(R.xml.preferences);
setupCustomPreferences();
runOnceAlarmManager();
setupAppDebugMode(_debug);
setupRateAppPreference();
setupImportPreferences();
runOnceEula();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在运行 Gingerbread,因此我正在查看 Android 源代码的 Gingerbread 版本。
您就会看到
如果您查看 PreferenceManager.java 的第 251 行, 此处,你会看到
如果你看一下充气机的代码此处,您会看到它会转换为
P
,在本例中为PreferenceScreen
。所以它试图将你的类转换为 PreferenceScreen,但正如你所说,它扩展了 Preference。是否可以让你的类扩展 PreferenceScreen? (仅从名称来看,它确实应该从一开始就做到这一点!)。
I assume that you're running Gingerbread, so I'm looking at the Gingerbread version of the Android source code.
You see
So if you look at line 251 of PreferenceManager.java here, you'll see
If you look at the code of the inflater here, you'll see that it trips over a cast to
P
, which in this case isPreferenceScreen
. So it's trying to cast your class to PreferenceScreen, but like you said, it extends Preference.Is it possible to have your class extend PreferenceScreen? (Just judging by the name alone, it really should do that to begin with!).