1

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.

musica
  • 1,373
  • 3
  • 15
  • 34
Noel Omondi
  • 551
  • 1
  • 6
  • 23
  • 1
    You called `color(0)`, and your error says it isn't happy about that – OneCricketeer Nov 16 '16 at 20:21
  • `mOnboardingAdapter.getItem(position + 1)` seems to have its attribute that is returned by `backgroundColor()` not initialized – OneCricketeer Nov 16 '16 at 20:21
  • Hi @cricket_007 could you please expound more on this, I have added a bit of more information to the question to make it more clear, how will I initialize the item at position + 1 ? – Noel Omondi Nov 17 '16 at 10:02
  • I'm just pointing out what the error says... The integer is 0, and that resource is not found. Add some Log statements to debug. I'm not sure you are even getting the color correctly http://stackoverflow.com/questions/5271387/get-color-int-from-color-resource – OneCricketeer Nov 17 '16 at 13:40

2 Answers2

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);
            }
        }
Noel Omondi
  • 551
  • 1
  • 6
  • 23
0

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)

Malik Abu Qaoud
  • 208
  • 1
  • 9
  • Hi could you please tell me how I would initialize this `mOnboardingAdapter.getItem(position + 1)` I have updated the question with more information, hope this helps – Noel Omondi Nov 17 '16 at 10:15
  • Ok can you tell me why you are using addOnPageChangeListener not OnPageChangeListener ?? – Malik Abu Qaoud Nov 17 '16 at 10:23
  • And i think its getItemPosition(position); not getItem() – Malik Abu Qaoud Nov 17 '16 at 10:26
  • getItemPosition() would return the position of the item, what I need is getItem(position) in order to return the fragment object at that position. The object can then be used to get the background color. – Noel Omondi Nov 17 '16 at 11:30
  • Ok @NoelOmondi then you have to pass the position using Instance, public static OnboardingFragment newInstance(SlideFragmentBuilder builder, int position) {} – Malik Abu Qaoud Nov 17 '16 at 12:07