TextEputLayout setError method throws a ClassCastException at 24.2.0

I upgraded the lib support version to 24.2.0 and my registration screen is already dead. The problem is in 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 get an error when I try to pass the parent EditText element to TextInputLayout, there is such code in the layout for this:

<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 fine, but now it throws a ClassCastException:

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

I am wondering if there are any new guidelines in this?

+4
source share
3 answers

, - , TextInputLayout → FrameLayout → TextInputEditText, :( , , :

@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;
}

TextInputLayout.

, , - , styles.xml:

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

, → MaterialEditTest, .

, ...

+1

TextInputLayout v24.2.x.
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);
    }
}

mInputFrame FrameLayout.

This is the cause of your problem (parent FrameLayout).

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

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

0
source

Source: https://habr.com/ru/post/1652493/


All Articles