TextInputLayout:RuntimeException - 无法解析索引24处的属性

时间:2016-03-12 10:41:41

标签: android android-edittext android-popupwindow android-textinputlayout

当我在setErrorEnabled上尝试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)

这是方案 - 我在textInputLayout内有一个popupwindow。这是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);
    }
}

这是popupwindow布局的xml:

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

这是我正在使用的风格:

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

弹出窗口在android.support.v4.app.Fragment内调用,片段位于AppCompatActivity内。

当用户单击“确定”按钮但名称edittext为空时,会出现此错误。换句话说,用户没有在edittext中输入任何内容并单击了确定:

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

如何让这个错误消失?

2 个答案:

答案 0 :(得分:57)

只需将TextLabel的基本主题更改为Widget.Design.TextInputLayout

<style name="TextLabel" parent="Widget.Design.TextInputLayout">

答案 1 :(得分:3)

对我来说,以下作品(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>