Viktor Yakunin Viktor Yakunin - 1 year ago 255
Android Question

TextInputLayout setError method throws ClassCastException in 24.2.0

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) {

protected void clearError(@NonNull EditText editText) {
TextInputLayout viewParent = (TextInputLayout) editText.getParent();

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:inputType="textCapWords" />

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

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

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

Answer Source

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:

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>