Skip to content Skip to sidebar Skip to footer

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 Cha

Solution 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:

@Nullableprivate TextInputLayout getTextInputLayout(@NonNull EditText editText) {
        ViewcurrentView= editText;
        for (inti=0; i < 2; i++) {
            ViewParentparent= currentView.getParent();
            if (parent instanceof TextInputLayout) {
                return (TextInputLayout) parent;
            } else {
                currentView = (View) parent;
            }
        }
        returnnull;
}

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:

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

Solution 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...

Solution 3:

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

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

    //....

}

@OverridepublicvoidaddView(View child, int index, final ViewGroup.LayoutParams params) {
    if (child instanceof EditText) {
        mInputFrame.addView(child, newFrameLayout.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.

Post a Comment for "Textinputlayout Seterror Method Throws Classcastexception In 24.2.0"