2

I have an OnClickListener on an image in my app. It allows the user to skip to a different part of the app if desired. The way the app runs, they can only use it 3 times.

My issue is, I want to get fancy pants. So I added an R.anim.fade_out animation to make the image fade out after all 3 times were used. I am using a counter decreased by one in another method.

The issue is, when the original method is recalled, it throws a Null Reference Exception because it can't find the image to set the OnClickListener. I tried wrapping it in an If/Else If statement:

if(skipsAllowed > 0){

            skipButton.setOnClickListener(new OnClickListener(){

                public void onClick(View v){

                    if(skipsAllowed > 0){
                        try {
                            skippedPage();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            });

        }else  if(skipsAllowed == 0){
            skipFadeOut = AnimationUtils.loadAnimation(null, android.R.anim.fade_out);
            skipButton.startAnimation(skipFadeOut);
        }

This still didn't work. Any ideas on how to stop this?

I instantiate the ImageView at the start of every new call to this Activity, should I be placing that inside my If/Else If?

ridoy
  • 6,274
  • 2
  • 29
  • 60
Phoenix
  • 1,881
  • 5
  • 20
  • 28

3 Answers3

10

Simple: skipButton.setOnClickListener(null)

msysmilu
  • 2,015
  • 23
  • 24
2

If skipsAllowed == 0 unregister onClickListener

See Remove an onclick listener

By the way, you should review your code, we usually set a click listener only once.

Community
  • 1
  • 1
Androiderson
  • 16,865
  • 6
  • 62
  • 72
  • Thanks for the link, that helped. You were right, I moved my click listener up to the `onCreate()`, and simply unregistered it when it was time. Thanks again! – Phoenix Jul 11 '13 at 20:23
2

You are getting an exception because you are passing null context to loadAnimation.

You can get application context with : getApplicationContext()

And after animation start you should set you button to INVISIBLE to completely hide the skip button.

Animation skipFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out);
        skipButton.startAnimation(skipFadeOut);
        skipButton.setVisibility(View.INVISIBLE);
ridoy
  • 6,274
  • 2
  • 29
  • 60
RicardoM
  • 136
  • 2
  • Thanks! I actually saw that right after posting, I should have edited the question. It all works like it should. – Phoenix Jul 11 '13 at 20:24