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添加到现有的输入类型.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...