找不到Android资源onPageScrolled异常 [英] Android resource not found exception onPageScrolled
问题描述
当尝试在onPageScrolled
方法中更改ViewPager
内页面的背景时,出现以下异常.为了使问题更清楚,我已经对问题进行了编辑.
I got the following exception when trying to change the background of pages inside a ViewPager
in the onPageScrolled
method. I have edited the question in order to make it more clear.
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1245)
at android.content.res.Resources.getColor(Resources.java:899)
at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:413)
at com.noel.material_onboarding.OnboardingActivity.color(OnboardingActivity.java:113)
at com.noel.material_onboarding.OnboardingActivity.access$200(OnboardingActivity.java:29)
at com.noel.material_onboarding.OnboardingActivity$1.onPageScrolled(OnboardingActivity.java:86)
首先,我创建滑块对象,其中包括设置背景色:
First I create the slider objects, this includes setting up the background color:
addSlide(new SlideFragmentBuilder()
.description("This is a test")
.backgroundColor(R.color.colorPrimary)
.build());
addSlide(new SlideFragmentBuilder()
.description("This is a test 2")
.backgroundColor(R.color.green)
.build());
addSlide(new SlideFragmentBuilder()
.description("This is a test 3")
.backgroundColor(R.color.orange)
.build());
addSlide(new SlideFragmentBuilder()
.description("This is a test 4")
.backgroundColor(R.color.orange)
.build());
这是 SlideFragmentBuilder 和这是我的onPageScrolled方法:
Here's my onPageScrolled method:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int colorUpdate = (Integer) evaluator.evaluate(positionOffset, color(mOnboardingAdapter.getItem(position).backgroundColor()), color(mOnboardingAdapter.getItem(position + 1).backgroundColor()));
mViewPager.setBackgroundColor(colorUpdate);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(colorUpdate);
}
}
@Override
public void onPageSelected(int position) {
btnFinish.setVisibility(position == mOnboardingAdapter.getLastItemPosition() ? View.VISIBLE : View.GONE);
btnNext.setVisibility(position == mOnboardingAdapter.getLastItemPosition() ? View.GONE : View.VISIBLE);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
使用的color()方法
The color() method that is used
private int color(@ColorRes int color){
return ContextCompat.getColor(this, color);
}
基本上,我只需要在用户滑动到另一页时淡出一页的背景即可.
Basically, I just need the background of one page to fade in as the user swipes to another page.
推荐答案
好,所以我浏览了文档,发现我错过了一个重要的东西:
Ok, so I went through the docs and found an important thing I was missing out on:
int:当前显示的第一页的位置索引.如果positionOffset不为零,则页面position + 1将可见.
int: Position index of the first page currently being displayed. Page position+1 will be visible if positionOffset is nonzero.
基本上,在positionOffset返回零之后,应用程序在第二个屏幕上崩溃了.看看这是如何工作的: 在第一个屏幕上,positionOffset为零,页面的位置也为零,但是由于positionOffset为零,所以position + 1不可用.我通过添加以下语句来检查偏移量"是否为零来解决此问题:
Basically the app was crushing on the second screen after the positionOffset went back to zero. See this is how it works: On the first screen the positionOffset is zero and the position of the page is also zero, however position + 1 is not available since the positionOffset is zero. I solved this by adding the following statement to check whether the Offset is zero or not:
positionOffset != 0.0 ? position + 1 : position
onPageScrolled方法的外观如下:
This is how the onPageScrolled method looks like:
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int colorUpdate = (Integer) evaluator.evaluate(positionOffset, color(mOnboardingAdapter.getItem(position).backgroundColor()), color(mOnboardingAdapter.getItem(positionOffset != 0.0 ? position + 1 : position).backgroundColor()));
mViewPager.setBackgroundColor(colorUpdate);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(colorUpdate);
}
}
这篇关于找不到Android资源onPageScrolled异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!