Android N在TextAppearanceSpan中崩溃 [英] Android N crashes in TextAppearanceSpan
问题描述
自从将Nexus 5X升级到Android N后,使用EditText时发生以下崩溃:
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)
当单击已经有一些文本的EditText时,会发生这种情况.我假设它是自动更正弹出窗口或类似的东西.
It happens when clicking on an EditText that has some text already. I am assuming it is the auto-correct popup or something similar.
我的应用使用了支持库24.2.0和Theme.AppCompat.Light.NoActionBar
My app uses support libs 24.2.0 and Theme.AppCompat.Light.NoActionBar
如果我除了在主题中添加colorAccent
之外,还添加android:colorAccent
,效果很好:
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>
但这不是必需的,因为我继承自Theme.AppCompat.
But this shouldn't be needed as I inherit from Theme.AppCompat.
我制作了一个展示问题的小应用程序:
I made a small app that showcases the problem:
推荐答案
在堆栈跟踪中,有对RecommendationionsPopupWindow的引用,这使我想到了禁用针对EditText的建议.
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);
}
}
我们也可以用XML设置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.
这篇关于Android N在TextAppearanceSpan中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!