Android N crashes in TextAppearanceSpan

二次信任 提交于 2019-11-30 14:01:34

问题


Since upgrading my Nexus 5X to Android N, I have the following crash when using EditText:

   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:528)
       at android.text.style.TextAppearanceSpan.<init>(TextAppearanceSpan.java:65)
       at android.text.style.TextAppearanceSpan.<init>(TextAppearanceSpan.java:45)
       at android.widget.Editor$SuggestionsPopupWindow.setUp(Editor.java:3316)
       at android.widget.Editor$PinnedPopupWindow.<init>(Editor.java:3016)
       at android.widget.Editor$SuggestionsPopupWindow.<init>(Editor.java:3309)
       at android.widget.Editor.replace(Editor.java:356)
       at android.widget.Editor$3.run(Editor.java:2129)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6077)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

It happens when clicking on an EditText that has some text already. I am assuming it is the auto-correct popup or something similar.

My app uses support libs 24.2.0 and Theme.AppCompat.Light.NoActionBar

Edit: It works fine if I add android:colorAccent in addition to just colorAccent in my theme:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/mainBrandColor</item>
    <item name="colorPrimaryDark">@color/mainBrandDarkerColor</item>
    <item name="colorAccent">@color/mainBrandColor</item>
    <item name="android:colorAccent">@color/mainBrandColor</item>
</style>

But this shouldn't be needed as I inherit from Theme.AppCompat.

I made a small app that showcases the problem:

https://github.com/martinbonnin/TextAppearanceSpanCrash/blob/master/app/src/main/java/mbonnin/com/textappearancescancrash/MainActivity.java


回答1:


In the stack trace there was a reference to SuggestionsPopupWindow that made me think of disabling suggestions for EditText.

I used the following code as a workaround:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        if ((editText.getInputType() & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) != InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) {
            editText.setInputType(editText.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        }
    }

We can also set inputType in XML, but the above code allows us to add TYPE_TEXT_FLAG_NO_SUGGESTIONS to existent input type.




回答2:


Updating the support library to 25.0.0 or higher fixes this issue




回答3:


Add this to your Edit text view android:textAppearance="@color/" like this:

<EditText 
      android:textAppearance="@color/abc_primary_text_disable_only_material_dark"
      ...
/>

"@color/abc_primary_text_disable_only_material_dark" (built in) :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/bright_foreground_disabled_material_dark"/>
    <item android:color="@color/bright_foreground_material_dark"/>
</selector>

it works for me



来源:https://stackoverflow.com/questions/39234832/android-n-crashes-in-textappearancespan

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