I have a TextInputEditText wrapped in a TextInputLayout. However on some keyboards, when the user has a spelling suggestion, and taps on the word to display the popup list of suggestions, my app crashes hard, leaving the keyboard on screen, sometimes overtop of the dialog.
This happens on Android 6, 7, and 8. I can replicate the issue with an Samsung Galaxy S5 running 6.0.1 and the stock Samsung keyboard, but not Gboard or Swype on the same device, and on a Nexus 5x running Android 8.0 using the default Google keyboard.
The application targets 26, and is using the design support library version 26.1.0. This same stack trace has shown up for us across several versions of the support library, and this, or a similar issue is claimed to have been fixed before.
It was pretty difficult to track down the cause and replicate it. I'm not setting any special themes or colors - just appcompat. Here's the stack trace:
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:484)
at android.text.style.TextAppearanceSpan.<init>(TextAppearanceSpan.java:65)
at android.text.style.TextAppearanceSpan.<init>(TextAppearanceSpan.java:45)
at android.widget.Editor$SuggestionsPopupWindow$SuggestionInfo.<init>(Editor.java:3012)
at android.widget.Editor$SuggestionsPopupWindow$SuggestionInfo.<init>(Editor.java:3007)
at android.widget.Editor$SuggestionsPopupWindow.initContentView(Editor.java:2995)
at android.widget.Editor$PinnedPopupWindow.<init>(Editor.java:2844)
at android.widget.Editor$SuggestionsPopupWindow.<init>(Editor.java:2969)
at android.widget.Editor.showSuggestions(Editor.java:2229)
at android.widget.Editor$2.run(Editor.java:2109)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Here's similar/related issues I was able to find:
Unfortunately it seems like the only workaround is to disable spelling suggestions for all users on TextInputEditText fields, which is pretty lame. I'm open to other ideas/suggestions.
In my case here, I had applied a custom textAppearance to the parent TextInputLayout:
<android.support.design.widget.TextInputLayout
android:textColorHint="@color/textColorPrimary"
android:textColor="@color/textColorPrimary"
android:theme="@style/smallHint"
android:hint="@string/add_a_comment"
android:layout_marginEnd="16dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="textImeMultiLine|textCapSentences"
android:importantForAutofill="noExcludeDescendants"
tools:ignore="UnusedAttribute"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
The theme definition looked like this:
<style name="smallHint" parent="TextAppearance.AppCompat">
...
</style>
This was not correct. TextInputLayout's theme should not be set to something with a parent of TextAppearance.AppCompat.
Instead of android:theme, I should have used app:hintTextAppearance.