最近,在使用 TextInputLayout
的时候发生了标题所示的异常,如果不打开 Logcat 一直都不会发现有异常产生,实际使用一切正常。
最后定位问题代码,发现主要是因为我在使用 TextInputLayout
的时候没有指定它的错误样式和提示样式。这时会抛出异常,但会自动搜寻默认方案来设置错误样式。
解决的办法也很简单:在 TextInputLayout
的 xml 文件中添加属性 app:errorTextAppearance
和 app:hintTextAppearance
,然后如下设置:
1
2
3
4
5
6
7
8
9
10
11
12
13
| <android.support.design.widget.TextInputLayout
style="@style/Widget.Design.TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/section_vertical_spacing"
app:errorTextAppearance="@style/TextAppearance.Design.Error"
app:hintTextAppearance="@style/TextAppearance.Design.Hint">
<!-- ... -->
</android.support.design.widget.TextInputLayout>
|
然后再在 /res/values/styles.xml
文件中设置如下内容:
1
2
3
4
5
6
| <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="textColorError">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorPrimary</item>
</style>
|
上述文件中的属性 textColorError
为错误信息及有错误时的下划线颜色,colorControlActivated
为提示信息及无错误时的下划线颜色。