自定义小部件无法强制转换为自定义小部件

时间:2015-05-15 08:21:56

标签: android android-edittext android-widget

我创建了一个扩展EditText的自定义窗口小部件,看起来像EditText,但行为有点不同,这是代码,虽然我不认为该类的内容是问题:

public class SalutationEditTextLikeButton extends EditText {

    CharSequence[] options;
    String selection;
    Context context;

    long performClickCatch = 0;

    public void dontPerformClickForMilliseconds(long milliseconds) {
        performClickCatch = System.currentTimeMillis() + milliseconds;
    }

    public SalutationEditTextLikeButton(Context context) {
        super(context);
        this.context = context;
        disableInput();
    }

    public SalutationEditTextLikeButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.context = context;
        disableInput();

    }

    public SalutationEditTextLikeButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        disableInput();
    }

    private void disableInput() {
        this.setCursorVisible(false);
        setKeyListener(null);
    }

    public String getSelection() {
        return selection;
    }

    public void setSelection(String selection) {
        this.selection = selection;
        this.setHint("");
        this.setText(selection);
        this.clearComposingText();

    }

    public void setOptions(CharSequence[] options) {
        this.options = options;
    }

    @Override
    public Parcelable onSaveInstanceState() {
        Log.d("butx", "save");

        return super.onSaveInstanceState();

    }

    @Override
    public boolean performClick() {

        if (System.currentTimeMillis() < performClickCatch) {
            return true;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(getResources().getString(R.string.anrede));

        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                setSelection(options[which].toString());
                setError(null);
                dialog.dismiss();

            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

        this.clearComposingText();

        return true;

    }
}

我像这样使用这个类:

final SalutationEditTextLikeButton salutationEditTextLikeButton
        = (SalutationEditTextLikeButton) LayoutInflater.from(context).inflate(R.layout.single_salutationbutton_w_style, null);

xml布局为:

<xy.SalutationEditTextLikeButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/EditTextStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

final SalutationEditTextLikeButton salutationEditTextLikeButton
        = (SalutationEditTextLikeButton) LayoutInflater.from(context).inflate(R.layout.single_salutationbutton_w_style, null);

我收到了关于Crittercism的罕见异常报告,但有以下例外:

    Caused by: java.lang.ClassCastException: 
xy.SalutationEditTextLikeButton cannot be cast to xy.SalutationEditTextLikeButton

这对我来说似乎很奇怪。 A偶尔与其他自定义小部件有相同的例外。

这只发生在三星的Galaxy S5(SM-G900F)上。

关于这意味着什么以及如何解决它的任何想法?

1 个答案:

答案 0 :(得分:1)

首先尝试为自定义视图提供ID:

<xy.SalutationEditTextLikeButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/salutationEditTextLikeButton"
    style="@style/EditTextStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

现在膨胀xml:

final View view = LayoutInflater.from(context).inflate(R.layout.single_salutationbutton_w_style, null);

使用自定义视图xml id:

从布局充气程序参考中投射自定义视图
final SalutationEditTextLikeButton salutationEditTextLikeButton = (SalutationEditTextLikeButton) view.findViewById(R.id.salutationEditTextLikeButton);