微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android N在TextAppearanceSpan中崩溃

自从将Nexus 5X升级Android N后,使用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)

单击已包含某些文本的EditText时会发生这种情况.我假设它是自动正确的弹出窗口或类似的东西.

我的应用程序使用支持库24.2.0和Theme.AppCompat.Light.NoActionBar

编辑:如果我添加android:colorAccent除了我的主题中的colorAccent之外它工作正常:

<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>

但是,当我从Theme.AppCompat继承时,不应该这样做.

我做了一个小应用程序,展示了这个问题:

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

解决方法:

在堆栈跟踪中有一个对SuggestionsPopupWindow的引用,这让我想到了禁用EditText的建议.

我使用以下代码作为解决方法

    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);
        }
    }

我们也可以在XML中设置inputType,但上面的代码允许我们将TYPE_TEXT_FLAG_NO_SUGGESTIONS添加到现有的输入类型.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐