Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x1 exception

Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x1 exception … here is a solution to the problem.

Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x1 exception

I’ve been following the myfirstapp guide on Android developer training, but I’m running into a problem they do not properly explain how to define colors.

They mention that to create a custom theme, you can declare the text color like this:

Subject .xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:textColor">@style/MyActionBarTitleText</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    </style>

<!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

<!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
        parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>

<!-- ActionBar tabs text styles -->
    <style name="MyActionBarTabText"
        parent="@android:style/Widget.Holo.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>
</resources>

They don’t mention how to specify @color/actionbar_text, but common sense (and some Google searches) suggests that a colors.xml file is needed in the value pack:

Color .xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_text">#ff355689</color>
</resources>

However, when trying to run the application, an error occurs:

Process: com.example.myfirstapp, PID: 25997
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1

If I remove the line from colors.xml, it gets an error when the color reference is not found. I’m relatively sure my code is correct, can anyone see any errors?

android studio showing error

Edit

I

just wanted to point out that I actually use a slightly different syntax for the themes.xml file because the syntax in the tutorial doesn’t compile. The tutorial uses @style/widget.Holo.ActionBar.TabText, which I found to be an Android property, so I needed to use @android: style/widget.Holo.ActionBar. TabText instead.

Solution

If I’m not mistaken, the code says Android found the reference when it expected the color value or couldn’t convert the reference to color. Looking at your code, this line stands out

<item name="android:textColor">@style/MyActionBarTitleText</item>

Although you can

reference it in textColor, I’m not sure if you can style it.

So try to quote your colors directly

<item name="android:textColor">@color/actionbar_text</item>

Related Problems and Solutions

Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x2 exception

Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x2 exception … here is a solution to the problem.

Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x2 exception

This exception is only thrown when extending EditText on two ASUS smartphones (Asus Zenfone 5 – API 19 and Asus Zenfone 2 – API 21). Other devices/emulators work fine.

Full stack trace:

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)

Layout

        <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>

Theme

<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>

Something I’ve already tried :

  • Replace TextInputEditText with EditText
  • Remove textColor=”@color/white” from EditText
  • Downgraded to support library 24,23. (Currently I’m at 25.0.1).

Solution

Thanks to this comment solves this problem by changing the parent of the text input layout theme to 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>

Related Problems and Solutions