java.lang.UnsupportedOperationException: Can't convert value at index 5 to color: type=0x5

久未见 提交于 2019-12-04 10:01:20

问题


My app is crashing after updating to Android Studio 3 RC1 (Gradle 4, Android Gradle plugin 3 RC2).

This is the error I am having:
java.lang.RuntimeException: Unable to start activity ComponentInfo{*****}:
android.view.InflateException: Binary XML file line #0: Binary XML file line #0:
Error inflating class com.viewpagerindicator.CirclePageIndicator
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
       at android.app.ActivityThread.-wrap11(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6541)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
    Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.viewpagerindicator.CirclePageIndicator
    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.viewpagerindicator.CirclePageIndicator
    Caused by: java.lang.reflect.InvocationTargetException
       at java.lang.reflect.Constructor.newInstance0(Native Method)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
       at android.view.LayoutInflater.createView(LayoutInflater.java:647)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.createCustomViewInternal(CalligraphyLayoutInflater.java:211)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.access$000(CalligraphyLayoutInflater.java:20)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$PrivateWrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:302)
       at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
       at com.raimbekov.android.sajde.zikr.ZikrActivity.onCreate(ZikrActivity.java:195)
       at android.app.Activity.performCreate(Activity.java:6975)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
       at android.app.ActivityThread.-wrap11(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6541)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
    Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 5 to color: type=0x5
    10-20 10:48:09.562 6156-6156 E/AndroidRuntime:     at android.content.res.TypedArray.getColor(TypedArray.java:473)
        at com.viewpagerindicator.CirclePageIndicator.<init>(CirclePageIndicator.java:96)
        at com.viewpagerindicator.CirclePageIndicator.<init>(CirclePageIndicator.java:72)
            ... 34 more

I didn't have this problem before update (on Android Studio 2). It is also described on github, but no answer.


回答1:


Change
compile 'com.viewpagerindicator:library:2.4.1@aar'

to
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'

It'll work. There is a problem with the library.




回答2:


My project uses android gradle plugin 3.0 and compileSdk 27. I wasn't able to make it work with this setup using the suggested solutions (compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1') in this thread.

It was complaining about methods like ViewPager#removeOnPageChangeListener not existing which have been added in version 24 of the support library. I think it has to do with the fact that the library has android-support-v4.jar directly baked in.

I didn't want to go through all my xml files and set the attributes programmatically so I ended up creating a fork that uses gradle to build the library and bumps compileSdk/minSdk/support-v4 to more modern versions.

You can find the project here https://github.com/splatte/ViewPagerIndicator

To use it in your project, get it from jitpack like so:

allprojects {
    repositories {
      maven { url "https://jitpack.io" }
    }
}

and then:

dependencies {
    implementation 'com.github.splatte:ViewPagerIndicator:3.0.0'
}

(There must be another project that does this in the sea of 4.300 forks that ViewPagerIndicator has, but I didn't know how to find it.)




回答3:


compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1' using jitpack.io didn't work for me with the android gradle plugin 3.0.

Instead I had to set programmatically all the view attributes that I previously set in the xml layout with cpi: (or app:) attributes.

Example

<com.viewpagerindicator.CirclePageIndicator
    cpi:fillColor="@color/azure"
    cpi:pageColor="@color/white"
    cpi:radius="4dp"
    cpi:snap="true"
    cpi:strokeWidth="0dp" />

I removed all the cpi: attributes and set them all programmatically:

circlePageIndicator.setFillColor(ContextCompat.getColor(getContext(), R.color.azure));
circlePageIndicator.setPageColor(ContextCompat.getColor(getContext(), R.color.white));
circlePageIndicator.setRadius((int) (4 * Resources.getSystem().getDisplayMetrics().density));
circlePageIndicator.setSnap(true);
circlePageIndicator.setStrokeWidth(0);



回答4:


I found the jitpack.io the easiest method:

allprojects {
    repositories {
      maven { url "https://jitpack.io" }
    }
}

and then:

dependencies {
  implementation ('com.github.JakeWharton:ViewPagerIndicator:2.4.1'){
    exclude module: 'support-v4'
  }
}

Just to note that having two versions of the support-v4 library broke it until I excluded one of them.



来源:https://stackoverflow.com/questions/46847724/java-lang-unsupportedoperationexception-cant-convert-value-at-index-5-to-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!