TextInputLayout setError method throws ClassCastException in 24.2.0

梦想的初衷 提交于 2019-12-19 20:44:33

问题


I updated support lib version to 24.2.0 and my registration screen is dead now. The problem is in the TextInputLayout, I have two methods:

    protected void setError(@Nullable CharSequence errorMsg, @NonNull EditText editText, boolean forceClean) {
        TextInputLayout viewParent = (TextInputLayout) editText.getParent();
        if (forceClean) {
            viewParent.setErrorEnabled(false);
            viewParent.setError(null);
        }
        viewParent.setErrorEnabled(true);
        viewParent.setError(errorMsg);
    }

    protected void clearError(@NonNull EditText editText) {
        TextInputLayout viewParent = (TextInputLayout) editText.getParent();
        viewParent.setErrorEnabled(false);
        viewParent.setError(null);
    }

I'm getting an error when I'm trying to cast the parent of EditText to TextInputLayout, in the layout I have this kind of code for that:

<android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
          android:id="@+id/login_registration_firstname"
          style="@style/registration_form_field"
          android:hint="@string/login_registration_firstname"
          android:inputType="textCapWords" />
</android.support.design.widget.TextInputLayout>

So, this worked perfectly fine, but now it throws ClassCastException:

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout

I'm wondering if there some new guides on that?


回答1:


The problem investigation showed that there is extra layout for some reason, so now we have TextInputLayout -> FrameLayout -> TextInputEditText and that is sad :( so as temp workaround I've created following method:

@Nullable
private TextInputLayout getTextInputLayout(@NonNull EditText editText) {
        View currentView = editText;
        for (int i = 0; i < 2; i++) {
            ViewParent parent = currentView.getParent();
            if (parent instanceof TextInputLayout) {
                return (TextInputLayout) parent;
            } else {
                currentView = (View) parent;
            }
        }
        return null;
}

This one will find your TextInputLayout in view hierarchy.

If you the lucky one you will notice this pink error color change, the easy way to overcome it is override style, in styles.xml:

<style name="TextAppearance.Design.Error" parent="TextAppearance.AppCompat.Caption" tools:override="true">
        <item name="android:textColor">@color/red</item>
</style>



回答2:


I was having the same issue with this and I ended up using that library -> MaterialEditTest who was far more reliable and offers more features.

With that one, you don't need to nest elements and you can modify small label color and error message...




回答3:


You can check the code of the TextInputLayout v24.2.x.
Now it works with a FrameLayout.

public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    // Can't call through to super(Context, AttributeSet, int) since it doesn't exist on API 10
    super(context, attrs);
    //...
    mInputFrame = new FrameLayout(context);
    mInputFrame.setAddStatesFromChildren(true);
    addView(mInputFrame);

    //....

}

@Override
public void addView(View child, int index, final ViewGroup.LayoutParams params) {
    if (child instanceof EditText) {
        mInputFrame.addView(child, new FrameLayout.LayoutParams(params));
        //...
     } else {
        // Carry on adding the View...
        super.addView(child, index, params);
    }
}

where mInputFrame is a FrameLayout.

It is the reason of your issue (the parent is a FrameLayout).

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout

You should use the TextInputLayout as parameter instead of navigating in the view hierarchy.



来源:https://stackoverflow.com/questions/39149787/textinputlayout-seterror-method-throws-classcastexception-in-24-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!