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

£可爱£侵袭症+ 提交于 2019-12-03 04:40:49
Abhilash Das

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.

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

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);
eatyourpeas

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.

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