为什么我的PreferenceScreen导致力量关闭?

时间:2011-08-04 06:18:17

标签: java android preferences

我正在让我的偏好看起来更好一些。我正在使用IconPreferenceScreen类为每个人添加图标,但是当我将所有PreferenceScreens更改为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();
}

1 个答案:

答案 0 :(得分:0)

我假设您正在运行Gingerbread,所以我正在查看Android源代码的Gingerbread版本。

你看

08-04 01:06:50.697: ERROR/AndroidRuntime(9683):     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)

因此,如果您查看PreferenceManager.java here的第251行,您会看到

   rootPreferences = (PreferenceScreen) inflater.inflate(resId, rootPreferences, true);

如果您查看inflater here的代码,您会看到它会通过强制转换为P,在这种情况下为PreferenceScreen。所以它试图将你的类转换为PreferenceScreen,但就像你说的那样,它扩展了Preference。

是否可以让您的课程扩展PreferenceScreen? (仅仅根据名称来判断,首先应该这样做!)。