【问题标题】:Custom widget cannot be cast to custom widget自定义小部件无法转换为自定义小部件
【发布时间】:2015-07-27 02:24:17
【问题描述】:

我创建了一个扩展 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) 上。

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

【问题讨论】:

  • 您可能有一份 SalutationEditTextLikeButton.java 的副本?

标签: android android-edittext android-widget


【解决方案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);

【讨论】:

  • 如果SalutationEditTextLikeButtonsingle_salutationbutton_w_style.xml 中的父视图,这会起作用吗?
  • 是的,但必须在 xml 中为 SalutationEditTextLikeButton 提供 id。
  • 来自View.findViewById() 源代码,是的,它会起作用(返回View)。但在他的情况下,您可以从崩溃日志中看到他得到了正确的Viewxy.SalutationEditTextLikeButton cannot be cast to xy.SalutationEditTextLikeButton。所以我认为这并不能解决问题。
猜你喜欢
  • 1970-01-01
  • 2014-06-24
  • 2012-08-06
  • 2011-11-05
  • 2020-10-15
  • 2016-05-17
  • 2019-03-28
  • 2019-04-01
  • 2020-11-23
相关资源
最近更新 更多