问题
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());
Here's a link to the SlideFragmentBuilder on github and the Fragment class itself
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) {
}
});
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.
回答1:
Ok, so I went through the docs and found an important thing I was missing out on:
int: Position index of the first page currently being displayed. Page position+1 will be visible if positionOffset is nonzero.
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
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);
}
}
回答2:
try to use this
Color.parseColor(mOnboardingAdapter.getItem(position + 1));
and don't forgot to remove integer cast
(Integer)
And i think that
mOnboardingAdapter.getItem(position + 1)
is a null value (not initialized)
来源:https://stackoverflow.com/questions/40641557/android-resource-not-found-exception-onpagescrolled