文本输入布局在setError方法上引发错误

时间:2017-10-11 07:50:48

标签: android android-textinputlayout android-textinputedittext

我在setError()TextInputLayout上应用TextInputEditText方法,但我收到此错误,

我想要实现的是错误工具提示。我没有心情使用任何第三方库来实现这一点。\

这是我尝试过的,但这些都没有效果,

Error in TextInputLayout dont show again even call setErrorEnabled(true)

TextInputLayout setErrorEnabled doesn't create new TextView object

TextInputLayout.setError() leaves empty space after clearing the error

TextInputLayout not showing error message after clearing

setError for TextInputLayout showing Error

TextInputLayout error after enter value into edittext

textinputlayout errorenabled and showing errors

textinputlayout errorenabled and showing errors

TextInputLayout's errorview is not removed when removing error message

摇篮

compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'

错误Stacktrace,

Process: com.example.demo, PID: 31847
                                                                       java.lang.UnsupportedOperationException: Failed to resolve attribute at index 24: TypedValue{t=0x3/d=0x1ec "res/color/secondary_text_material_dark.xml" a=1 r=0x1060120}
                                                                           at android.content.res.TypedArray.getColor(TypedArray.java:449)
                                                                           at android.widget.TextView.<init>(TextView.java:741)
                                                                           at android.widget.TextView.<init>(TextView.java:674)
                                                                           at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
                                                                           at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
                                                                           at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:54)
                                                                           at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:602)
                                                                           at com.example.demo.DemoActivity.canLogin(DemoActivity.java:163)
                                                                           at com.example.demo.DemoActivity.access$000(DemoActivity.java:24)
                                                                           at com.example.demo.DemoActivity$1.onClick(DemoActivity.java:54)
                                                                           at android.view.View.performClick(View.java:5207)
                                                                           at android.view.View$PerformClick.run(View.java:21177)
                                                                           at android.os.Handler.handleCallback(Handler.java:742)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5527)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

这是XML

<android.support.design.widget.TextInputLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="UserName"
        android:theme="@style/txtIptLayout"
       android:layout_marginTop="10dp"
       app:backgroundTint="@color/white"
        android:id="@+id/tilUsername"
       app:errorTextAppearance="@style/Theme.AppCompat"
       >
     <android.support.design.widget.TextInputEditText
         style="@style/txtEditTxtLayout"
         app:backgroundTint="@color/white"
         android:id="@+id/etUserName"
         android:maxLines="1"
         android:inputType="text"

         />

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

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:theme="@style/txtIptLayout"
        app:passwordToggleEnabled="true"
        app:passwordToggleDrawable="@drawable/ic_widget_icon_txtiptlayout"
        app:passwordToggleTint="@color/white"
        android:layout_marginTop="10dp"
        app:backgroundTint="@color/white"
       app:errorTextAppearance="@style/Theme.AppCompat"
        android:id="@+id/tilPassword"
        >
        <android.support.design.widget.TextInputEditText
           style="@style/txtEditTxtLayout"
            android:inputType="textPassword"
            android:id="@+id/etPassword"
            app:backgroundTint="@color/white"


            />

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

这是我正在应用的内容,这就是错误引发的地方,

tilPassword.setError("Password required");
                tilPassword.setErrorEnabled(true);

3 个答案:

答案 0 :(得分:0)

试试这个:

  tilPassword.getEditText().setError("Password required");
  tilPassword..setErrorEnabled(true);

答案 1 :(得分:0)

不确定如何删除XML android:theme="@style/txtIptLayout"中的主题属性。假设从XML覆盖主题将覆盖工具提示的主题,从而覆盖错误。

答案 2 :(得分:0)

这会发生,当您尝试从XML覆盖主题时,它也会覆盖默认功能。我在创建样式育儿TextAppearance.AppCompat时遇到了类似的问题。

Theme.AppCompat代替它解决了我的问题。