gpt4 book ai didi

Android N 在 TextAppearanceSpan 中崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:03:23 25 4
gpt4 key购买 nike

自从将我的 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 就可以正常工作:

<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 N 在 TextAppearanceSpan 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com