私は自分のPreference
クラスを構築しようとしていますが、少し問題があります。Preference
データを設定に保存する方法は、クラス内のメソッドの「永続化」グループを介するように見えます。ただし、私の好みでは、カラーピッカーダイアログを開き、ダイアログのcolorChanged
オーバーライド内から設定を保存する必要があります。アプリを実行して色の設定を変更しようとすると、次のようになります。
06-05 10:21:46.396: ERROR/AndroidRuntime(516): FATAL EXCEPTION: main
java.lang.IllegalAccessError: tried to access method android.preference.Preference.persistInt:(IIII)V from class android.preference.ColorSelectionPreference$1
at android.preference.ColorSelectionPreference$1.colorChanged(ColorSelectionPreference.java:55)
at android.apis.graphics.ColorPickerDialog.onClick(ColorPickerDialog.java:168)
(更新:6/5/12 12:20)を使用callChangeListener
して強制的onPreferenceChangeListener
にトリガーしようとしましたが、同じエラーでクラッシュします。がないcallChangeListener
と、設定データは(おそらく)保存されますが、onPreferenceChangeListener
トリガーされません:
06-05 12:20:23.691: ERROR/AndroidRuntime(2834): FATAL EXCEPTION: main
java.lang.IllegalAccessError: tried to access method android.preference.ColorSelectionPreference.callChangeListener:(IIII)V from class android.preference.ColorSelectionPreference$1
at android.preference.ColorSelectionPreference$1.colorChanged(ColorSelectionPreference.java:52)
at android.apis.graphics.ColorPickerDialog.onClick(ColorPickerDialog.java:168)
実際のクラスは次のとおりです。
package android.preference;
import android.apis.graphics.ColorPickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.AttributeSet;
public class ColorSelectionPreference extends Preference {
private Context mContext;
private int mColor;
public ColorSelectionPreference(Context context) {
super(context);
mContext = context;
}
public ColorSelectionPreference(Context context, AttributeSet attr) {
super(context, attr);
mContext = context;
}
public int getColor() {
return mColor;
}
public void setColor(int color) {
mColor = color;
}
@Override
public void onClick() {
//get original preference
//set ColorPickerDialog to original preference color or default color
ColorPickerDialog dialog = new ColorPickerDialog(mContext, new ColorPickerDialog.OnColorChangedListener() {
public void colorChanged(int a, int r, int g, int b) {
int selectedColor = Color.argb(a,r,g,b);
setColor(selectedColor);
/*** crashes on callChangeListener ***/
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
//SharedPreferences.Editor edit = prefs.edit();
//edit.putInt(getKey(), selectedColor);
//edit.commit();
//callChangeListener(selectedColor);
/*** the offending code, error refers to this line ***/
persistInt(selectedColor);
/*** tried this as well by request on IRC ***/
//ColorSelectionPreference.this.persistInt(selectedColor);
}
}, mColor);
dialog.show();
}
}