首选项对话框:NumberPicker ClassCast异常

时间:2014-07-21 04:11:59

标签: casting android-preferences numberpicker

只是尝试在我的偏好中运行NumberPicker ..在这里查看代码,但总是得到 java.lang.ClassCastException:java.lang.Long无法强制转换为java.lang.Integer this.persistInt(时间) ..

非常感谢!

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;

public class NumberPickerPreference extends DialogPreference {


    private int number = 0;
    private NumberPicker picker = null;;

    public NumberPickerPreference(final Context context,
            final AttributeSet attrs) {
        super(context, attrs);

        this.setPositiveButtonText("Set");
        this.setNegativeButtonText("Cancel");
    }

    @Override
    protected View onCreateDialogView() {
        this.picker = new NumberPicker(this.getContext());
        return (this.picker);
    }

    @Override
    protected void onBindDialogView(final View view) {
        super.onBindDialogView(view);

        this.picker.setMinValue(1);
        this.picker.setMaxValue(100);

    }

    @Override
    protected void onDialogClosed(final boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult) {

            this.number = this.picker.getValue();

            final int time = Integer.valueOf(this.number);

            if (this.callChangeListener(time)) {
                this.persistInt(time);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你为什么打电话

        final int time = Integer.valueOf(this.number);

而不仅仅是

        final int time = this.number;

此外,这是一个工作的NumberPickerPreference示例(它保存1到100之间的整数值):

screenshot

NumberPickerPreference.java

public class NumberPickerPreference extends DialogPreference {
    private NumberPicker mPicker;
    private Integer mNumber = 0;

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        TextView title = (TextView) view.findViewById(android.R.id.title);
        if (title != null) {
            title.setSingleLine(false);
            title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        }       
    }

    @Override
    protected View onCreateDialogView() {
        mPicker = new NumberPicker(getContext());
        mPicker.setMinValue(1);
        mPicker.setMaxValue(100);
        mPicker.setValue(mNumber);
        return mPicker;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            // needed when user edits the text field and clicks OK
            mPicker.clearFocus();
            setValue(mPicker.getValue());
        }
    }   

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mNumber) {
            mNumber = value;
            notifyChanged();
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, 0);
    }
}