java.lang.UnsupportedOperationException:无法转换为color:type = 0x2

时间:2016-11-28 20:32:25

标签: java android

只有在两台华硕智能手机(华硕Zenfone 5 - API 19和华硕Zenfone 2 - API 21)上充气EditText时才会抛出此异常。其他设备/模拟器工作正常。

完整堆栈跟踪:

Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.example/com.package.example.activity.SignUpActivity}: android.view.InflateException: Binary XML file line #47: Error inflating class EditText
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2318)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2396)
android.app.ActivityThread.access$800 (ActivityThread.java:139)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1293)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:149)
android.app.ActivityThread.main (ActivityThread.java:5257)
java.lang.reflect.Method.invokeNative (Method.java)
java.lang.reflect.Method.invoke (Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:609)
dalvik.system.NativeStart.main (NativeStart.java)
arrow_drop_down
Caused by android.view.InflateException: Binary XML file line #47: Error inflating class EditText
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:713)
android.view.LayoutInflater.rInflate (LayoutInflater.java:755)
android.view.LayoutInflater.rInflate (LayoutInflater.java:758)
android.view.LayoutInflater.rInflate (LayoutInflater.java:758)
android.view.LayoutInflater.rInflate (LayoutInflater.java:758)
android.view.LayoutInflater.parseInclude (LayoutInflater.java:839)
android.view.LayoutInflater.rInflate (LayoutInflater.java:745)
android.view.LayoutInflater.inflate (LayoutInflater.java:492)
uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate (CalligraphyLayoutInflater.java:60)
android.view.LayoutInflater.inflate (LayoutInflater.java:397)
android.view.LayoutInflater.inflate (LayoutInflater.java:353)
android.support.v7.app.AppCompatDelegateImplV9.setContentView (AppCompatDelegateImplV9.java:284)
android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:143)
com.package.example.activity.SignUpActivity.onCreate (SignUpActivity.java:88)
android.app.Activity.performCreate (Activity.java:5411)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1087)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2270)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2396)
android.app.ActivityThread.access$800 (ActivityThread.java:139)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1293)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:149)
android.app.ActivityThread.main (ActivityThread.java:5257)
java.lang.reflect.Method.invokeNative (Method.java)
java.lang.reflect.Method.invoke (Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:609)
dalvik.system.NativeStart.main (NativeStart.java)
arrow_drop_down
Caused by java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
android.content.res.TypedArray.getColor (TypedArray.java:327)
android.widget.TextView.<init> (TextView.java:700)
android.widget.EditText.<init> (EditText.java:61)
android.support.v7.widget.AppCompatEditText.<init> (AppCompatEditText.java:62)
android.support.v7.widget.AppCompatEditText.<init> (AppCompatEditText.java:58)
android.support.v7.app.AppCompatViewInflater.createView (AppCompatViewInflater.java:112)
android.support.v7.app.AppCompatDelegateImplV9.createView (AppCompatDelegateImplV9.java:1017)
android.support.v7.app.AppCompatDelegateImplV9.onCreateView (AppCompatDelegateImplV9.java:1076)
android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView (LayoutInflaterCompatHC.java:44)
uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView (CalligraphyLayoutInflater.java:280)
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:684)
android.view.LayoutInflater.rInflate (LayoutInflater.java:755)
android.view.LayoutInflater.rInflate (LayoutInflater.java:758)
android.view.LayoutInflater.rInflate (LayoutInflater.java:758)
android.view.LayoutInflater.rInflate (LayoutInflater.java:758)
android.view.LayoutInflater.parseInclude (LayoutInflater.java:839)
android.view.LayoutInflater.rInflate (LayoutInflater.java:745)
android.view.LayoutInflater.inflate (LayoutInflater.java:492)
uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate (CalligraphyLayoutInflater.java:60)
android.view.LayoutInflater.inflate (LayoutInflater.java:397)
android.view.LayoutInflater.inflate (LayoutInflater.java:353)
android.support.v7.app.AppCompatDelegateImplV9.setContentView (AppCompatDelegateImplV9.java:284)
android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:143)
com.package.example.activity.SignUpActivity.onCreate (SignUpActivity.java:88)
android.app.Activity.performCreate (Activity.java:5411)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1087)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2270)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2396)
android.app.ActivityThread.access$800 (ActivityThread.java:139)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1293)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:149)
android.app.ActivityThread.main (ActivityThread.java:5257)
java.lang.reflect.Method.invokeNative (Method.java)
java.lang.reflect.Method.invoke (Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:609)
dalvik.system.NativeStart.main (NativeStart.java)

布局

        <android.support.design.widget.TextInputLayout
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            app:errorTextAppearance="@style/AppTheme.Error"
            app:hintEnabled="true"
            app:theme="@style/AppTheme.TextInput.White">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/nickname"
                android:textColor="@color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_username"
                android:inputType="textPersonName"
                android:maxLines="1" />

        </android.support.design.widget.TextInputLayout>

主题

<style name="AppTheme.TextInput.White" parent="TextAppearance.AppCompat">
    <item name="colorControlNormal">@color/whiteC</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="android:textColorHighlight">@color/whiteC</item>
    <item name="android:textColorLink">@color/whiteC</item>
    <item name="android:textColorHint">@color/whiteC</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!--Error-->
<style name="AppTheme.Error" parent="AppTheme.Text.White">
    <item name="android:textSize">12sp</item>
    <item name="android:textStyle">italic</item>
</style>

我已经尝试过了:

  • 用EditText替换TextInputEditText
  • 从EditText
  • 中删除textColor =“@ color / white”
  • 降级以支持图书馆24,23。 (目前我在25.0.1)

2 个答案:

答案 0 :(得分:3)

感谢this comment问题通过将文字输入布局的主题更改为ThemeOverlay.AppCompat.Light来解决:

<style name="AppTheme.TextInput.White" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorControlNormal">@color/whiteC</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="android:textColorHighlight">@color/whiteC</item>
    <item name="android:textColorLink">@color/whiteC</item>
    <item name="android:textColorHint">@color/whiteC</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!--Error-->
<style name="AppTheme.Error" parent="AppTheme.Text.White">
    <item name="android:textSize">12sp</item>
    <item name="android:textStyle">italic</item>
</style>

答案 1 :(得分:1)

我遇到了同样的异常,但原因与 OP 问题完全不同,它只出现在 API 级别 21 以下。原因是在布局中使用了 ShapeableImageView;我在里面没有特殊的属性,但是一旦使用普通的ImageView,异常就消失了。

这是我使用的 ShapeableImageView

<com.google.android.material.imageview.ShapeableImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:padding="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/btn_change"
    app:layout_constraintTop_toTopOf="parent" />