RuntimeException when input exceeds counterMaxLength of TextInputLayout

纵然是瞬间 提交于 2019-12-10 18:23:28

问题


I have a form with a TextInputLayout and TextInputEditText. This is the relevant 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>

When I try to input the 17th character into this field my activity crashes with the following stacktrace:

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)

Apparently it has to do with not using the AppCompat Theme, but I am using the AppCompat theme already:

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

Not sure if this is relevant but I'm also observing this TextInputEditText using Jake Wharton's RxBinding library, RxTextView.textChanges(nameEditText);.


回答1:


I suggest adding additional two attributes and giving it a style. one is app:counterTextAppearance and another is app:counterOverflowTextAppearance like here,

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

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

Those two styles are nothing but simply an item with android:textColor name like for example,

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

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

See the full explanation here.

If that didn't work then I suggest extending the Theme from Theme.Design.* as suggested in this answer.



来源:https://stackoverflow.com/questions/37416530/runtimeexception-when-input-exceeds-countermaxlength-of-textinputlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!