致命异常:java.lang.UnsupportedOperationException:无法解析索引 6 处的属性:TypedValue{t=0x2/d=0x101009b a=1}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41727729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:06:43  来源:igfitidea点击:

Fatal Exception: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}

javaandroid

提问by Sandeep Gahlawat

I am seeing this error in a live app thus I have no clue why this happening. All I have is this exception log (see below). At first, I thought I applied style editTextBackgroungWhite(see below)to the editText inside my XML but removing it has no effect on the problem.

我在实时应用程序中看到此错误,因此我不知道为什么会发生这种情况。我只有这个异常日志(见下文)。起初,我以为我将样式 editTextBackgroungWhite(见下文)应用于我的 XML 中的 editText,但删除它对问题没有影响。

I'll be glad if someone could guide me towards the right direction.

如果有人能引导我走向正确的方向,我会很高兴。

P.S I am not able to replicate this bug on my device seems that it's a problem with some Samsung note series device only.

PS 我无法在我的设备上复制此错误,这似乎只是某些三星 note 系列设备的问题。

 <style name="editTextBackgWhite"parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">#ffffff</item>
    <item name="colorControlActivated">#ffffff</item>
    <item name="colorControlHighlight">#ffffff</item>
</style>


Fatal Exception: 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:484)
   at android.text.style.TextAppearanceSpan.(TextAppearanceSpan.java)
   at android.text.style.TextAppearanceSpan.(TextAppearanceSpan.java)
   at android.widget.Editor$SuggestionsPopupWindow$SuggestionInfo.(Editor.java)
   at android.widget.Editor$SuggestionsPopupWindow$SuggestionInfo.(Editor.java)
   at android.widget.Editor$SuggestionsPopupWindow.initContentView(Editor.java:2995)
   at android.widget.Editor$PinnedPopupWindow.(Editor.java)
   at android.widget.Editor$SuggestionsPopupWindow.(Editor.java)
   at android.widget.Editor.showSuggestions(Editor.java:2229)
   at android.widget.Editor.run(Editor.java:2109)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:7225)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

回答by Mr-IDE

The main cause of this problem seems to be a missing style/theme in the AppCompatlibrary, or a bug in the library. It happens when using either AppCompatEditTextor AppCompatAutoCompleteTextViewand a WebViewis also added to the layout (AdMob ads are WebViews). It happens on several versions of AppCompat, like 23, 24, 25, 26.

这个问题的主要原因似乎是AppCompat库中缺少样式/主题,或者是库中的错误。当使用AppCompatEditTextAppCompatAutoCompleteTextView并且WebView也添加到布局时会发生这种情况(AdMob 广告是 WebView)。它发生在 AppCompat 的多个版本上,例如 23、24、25、26。

It can be seen more clearly in a useful demo appthat someone made on Github:

在某人在 Github 上制作的一个有用的演示应用程序中可以更清楚地看到:

  1. Clone the repo and open in Android Studio v2.3 or higher.
  2. Android Studio should prompt to update the gradle dependencies and buildToolsVersion in this project, something like this:
  1. 克隆 repo 并在 Android Studio v2.3 或更高版本中打开。
  2. Android Studio 应该会提示更新此项目中的 gradle 依赖项和 buildToolsVersion,如下所示:

build.gradle:

构建.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    ...
}

app/build.gradle:

应用程序/build.gradle:

buildToolsVersion '25.0.0'

gradle-wrapper.properties:

gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
  1. Update the dependencies then build the project. Run the app on Android 7 emulator or device, with a stock Android keyboard. Set the system language to English.
  2. On the input field, type the letters 'tttf'on the Android keyboard. Then press space. The word should be underlined as misspelled.
  3. Now click one of the letters(not long-press) to try to move the cursor. It should crash the app - like this:
  1. 更新依赖项,然后构建项目。在 Android 7 模拟器或设备上运行该应用程序,并使用普通的Android 键盘。将系统语言设置为英语。
  2. 在输入字段中,在 Android 键盘上输入字母“tttf”。然后按空格键。这个词应该加下划线作为拼写错误
  3. 现在单击其中一个字母(不要长按)以尝试移动光标。它应该使应用程序崩溃 - 像这样:

Video screenshot example of the app crash

应用程序崩溃的视频截图示例

The solution (from here) is to explicitly add both"colorAccent"and "android:colorAccent"in your main AppTheme, in styles.xml:

(从该解决方案在这里)是明确添加"colorAccent""android:colorAccent"在主AppTheme,在styles.xml:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorAccent" tools:targetApi="lollipop">@color/colorAccent</item>
</style>

or

或者

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorAccent" tools:targetApi="lollipop">@color/colorAccent</item>
</style>

This will now work to allow the stock spelling suggestions popup to appear, and no more crashing.

这现在将允许出现股票拼写建议弹出窗口,并且不再崩溃。

Similar reports or duplicates:

类似的报告或重复:

回答by Abhinav Pawar

Had exact same exception while using AutoCompleteTextView. Replication was really tough and wasted my two days in this.

使用 AutoCompleteTextView 时出现完全相同的异常。复制真的很艰难,浪费了我两天的时间。

The cause for this is Google spell checker popup, enabled in some devices which causes red line under words in Edittext/AutoCompleteEdittext and clicking on it opens a popup of spell suggestions. According to my observations this popup doesn't go too well in TextInputLayout(with style) and causes crash in some devices (in my case it was Moto G play). You can verify it by turning off the Google spell checker and it will work fine. But the real solution is to add

造成这种情况的原因是Google 拼写检查弹出窗口,在某些设备中启用会导致 Edittext/AutoCompleteEdittext 中的单词下出现红线,点击它会打开拼写建议的弹出窗口。根据我的观察,此弹出窗口在 TextInputLayout(带样式)中不太好,并导致某些设备崩溃(在我的情况下是 Moto G 播放)。您可以通过关闭 Google 拼写检查器来验证它,它会正常工作。但真正的解决方案是添加

android:inputType="textNoSuggestions"

android:inputType="textNoSuggestions"

to your edittext in XML

到 XML 中的编辑文本

This disables the spell checker for that edittext and rest works fine.

这将禁用该编辑文本的拼写检查器,其余工作正常。

Let me know if it helped.

如果有帮助,请告诉我。

回答by Coccoonx

according to the answer of @Abhinav Pawar, I used

根据@Abhinav Pawar 的回答,我使用了

android:inputType="textAutoCorrect"

android:inputType="textAutoCorrect"

instead of

代替

android:inputType="textNoSuggestions"

android:inputType="textNoSuggestions"

In fact, textNoSuggestionsremove all suggested words by the keyboard at typing. While textAutoCorrectreplace the wrong word by the good one. And the user is free to use the one he wants.

事实上,textNoSuggestions 会在打字时通过键盘删除所有建议的单词。而textAutoCorrect用好的词替换错误的词。用户可以自由使用他想要的。