自定义pref getString-android

时间:2014-01-26 21:05:36

标签: android preferences

我有一个自定义的Pref:

public class ImagePreference2 extends Preference {
    private Drawable mIcon;
    Bitmap a;

    public ImagePreference2(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);

        this.setLayoutResource(R.layout.imagepref2);

        }
}

我已添加如下:

<en.cold.ImagePreference2
        android:key="test02"
        android:summary="ImagePreference with default icon"
        android:title="ImagePreference" 
        android:selectable="false"
        android:defaultValue="defaultV"
        />

我正在尝试从PreferencesActivity获取字符串值:

@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        Log.d("StripedLog",sharedPreferences.getString("test02", "iv"));
    }

我收到错误:

  01-26 16:00:35.031: E/AndroidRuntime(22564): FATAL EXCEPTION: main
    01-26 16:00:35.031: E/AndroidRuntime(22564): java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at en.cold.Settings.onSharedPreferenceChanged(Settings.java:130)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:475)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:385)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.preference.Preference.tryCommit(Preference.java:1357)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.preference.Preference.persistBoolean(Preference.java:1623)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.preference.TwoStatePreference.setChecked(TwoStatePreference.java:83)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:69)
    01-26 16:00:35.031: E/AndroidRuntime(22564):    at android.preference.Preference.performClick(Preference.java:957)
    01-26 16:00:35.031: E/AndroidRuntime(22564

我可以使用custompref获取字符串吗?

0 个答案:

没有答案