输入超出TextInputLayout的counterMaxLength时发生RuntimeException

我有一个带有TextInputLayout和TextInputEditText的表单.这是相关的XML

<android.support.design.widget.TextInputLayout
        android:id="@+id/signup_til_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:hint="Name"
        app:counterEnabled="true"
        app:counterMaxLength="16"
        app:errorEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/signup_etext_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"/>
    </android.support.design.widget.TextInputLayout>

当我尝试在该字段中输入第17个字符时,我的活动因以下堆栈跟踪而崩溃:

java.lang.RuntimeException: Failed to resolve attribute at index 3
at android.content.res.TypedArray.twGetColorStateList(TypedArray.java:438)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:420)
at android.widget.TextView.setTextAppearance(TextView.java:3029)
at android.support.design.widget.TextInputLayout.updateCounter(TextInputLayout.java:688)
at android.support.design.widget.TextInputLayout.access$300(TextInputLayout.java:84)
at android.support.design.widget.TextInputLayout$1.afterTextChanged(TextInputLayout.java:248)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8929)

显然,这与不使用AppCompat主题有关,但是我已经在使用AppCompat主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>

不知道这是否相关,但我还将使用Jake Wharton的RxBinding库RxTextView.textChanges(nameEditText);来观察此TextInputEditText.

解决方法:

我建议添加其他两个属性并为其指定样式.一个是app:counterTextAppearance,另一个是app:counterOverflowTextAppearance,如下所示,

<android.support.design.widget.TextInputLayout
  ....
  app:counterTextAppearance="@style/counterText"
  app:counterOverflowTextAppearance="@style/counterOverride">

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

这两种样式不过是带有android:textColor名称的项目,例如,

<style name="counterText">
  <item name="android:textColor">#aa5353cc</item>
</style>

<style name="counterOverride">
  <item name="android:textColor">#ff0000</item>
</style>

请参阅完整说明here.

如果那不起作用,那么我建议按照this answer中的建议从Theme.Design.*扩展主题.

上一篇:异常和单例


下一篇:第2篇 Scrum 冲刺博客