TextInputLayout: RuntimeException - Failed to reso

2019-02-11 21:01发布

I keep on getting this error when I try to setErrorEnabled on my textInputLayout :

03-12 12:29:03.206 5706-5706/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.myapp, PID: 5706
                                                 java.lang.RuntimeException: Failed to resolve attribute at index 24
                                                     at android.content.res.TypedArray.getColor(TypedArray.java:401)
                                                     at android.widget.TextView.<init>(TextView.java:696)
                                                     at android.widget.TextView.<init>(TextView.java:632)
                                                     at android.widget.TextView.<init>(TextView.java:628)
                                                     at android.widget.TextView.<init>(TextView.java:624)
                                                     at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380)
                                                     at com.bekwaai.popupwindow.RGNamePopUp$1.onClick(RGNamePopUp.java:48)
                                                     at android.view.View.performClick(View.java:4780)
                                                     at android.view.View$PerformClick.run(View.java:19866)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:135)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

This is the scenario - I have a textInputLayout inside a popupwindow. This is the code inside the popupwidow.

public class NamePopUp extends PopupWindow {

    android.support.v7.widget.AppCompatEditText name;
    TextInputLayout nameInput;


    public NamePopUp(final Context context) {
        super(context);
        View popupView = LayoutInflater.from(context).inflate(R.layout.popup_rg_confirm, null);
        nameInput = (TextInputLayout) popupView.findViewById(R.id.name_input_layout);
    }
}

This is my xml for the popupwindow layout:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:theme="@style/TextLabel"
    app:backgroundTint="@color/white"
    app:rippleColor="@color/white"
    android:id="@+id/name_input_layout"
    android:layout_marginBottom="8dp"
    android:paddingRight="24dp"
    android:paddingLeft="24dp">

<android.support.v7.widget.AppCompatEditText
    android:id="@+id/enter_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textCursorDrawable="@drawable/color_cursor"
    android:hint="@string/groupname"/>

</android.support.design.widget.TextInputLayout>

This is the style I'm using:

<style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textSize">20sp</item>
    <item name="android:textColorHint">@color/white</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/white</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/white</item>
</style>

The popupwindow is called inside a android.support.v4.app.Fragment and the fragment is inside a AppCompatActivity.

The error occurs here when the user clicks the ok button but the name edittext is blank. In other words, the user has not entered anything in the edittext and have clicked ok:

    button_ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (StringUtils.isNotEmpty(name.getText().toString())) {
                nameInput.setErrorEnabled(false);
                groupNameEvent.doEvent(name.getText().toString());

            } else {
                nameInput.setErrorEnabled(true); //ERROR OCCURS HERE!
                nameInput.setError(context.getString(R.string.no_name));
            }
        }
    });

How do I get this error to disappear?

2条回答
唯我独甜
2楼-- · 2019-02-11 21:47

Just change the base theme for TextLabel to Widget.Design.TextInputLayout

<style name="TextLabel" parent="Widget.Design.TextInputLayout">
查看更多
【Aperson】
3楼-- · 2019-02-11 22:03

For me a following works (https://stackoverflow.com/a/42779409/2914140):

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="textColorError">@color/design_textinput_error_color_light</item>
</style>
查看更多
登录 后发表回答