0

私は自分の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();
    }
}
4

2 に答える 2

0

これは、ハンドラーを使用して内部クラスからメインクラスにコールバックするハッキーな回避策です。きれいではありませんが、機能します。

package android.preference;

import android.apis.graphics.ColorPickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;

public class ColorSelectionPreference extends Preference {
    private Context mContext;
    private int mColor;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.getData().containsKey("color")) {
                int color = msg.getData().getInt("color");
                setColor(color);
            }
        }
    };

    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;
        persistInt(new Integer(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);
                Bundle bundle = new Bundle();
                bundle.putInt("color", selectedColor);
                Message msg = new Message();
                msg.setData(bundle);
                mHandler.sendMessage(msg);

                //setColor(selectedColor);

                /*** tried this, but the onPreferenceChangedListener never gets triggered, so this won't work ***/
                //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 ***/
                //container.

                /*** tried this as well by request on IRC ***/
                //ColorSelectionPreference.this.persistInt(selectedColor);
            }
        }, mColor);
        dialog.show();
    }
}
于 2012-06-05T18:00:34.307 に答える
0

私はこの問題に自分で遭遇しました。私はそれを理解するふりをしませんが、これはより簡単な修正のようです:

// ...why is this necessary?  what is special about Preference.persistInt?
@Override protected boolean persistInt(int value)
{
    return super.persistInt(value);
}
于 2013-05-09T14:22:30.647 に答える