Why does my PreferenceScreen cause a force close?
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 开发者_Python百科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();
}
I assume that you're running Gingerbread, so I'm looking at the Gingerbread version of the Android source code.
You see
08-04 01:06:50.697: ERROR/AndroidRuntime(9683): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
So if you look at line 251 of PreferenceManager.java here, you'll see
rootPreferences = (PreferenceScreen) inflater.inflate(resId, rootPreferences, true);
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 is PreferenceScreen
. 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!).
精彩评论