Need help fixing my class: 'This Handler class should be static or leaks might occur'
<p>I created a simple crossfade image class for use in my app. But... i have an error i can't fix due to lack of knowledge. I found this post <a href="http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler">This Handler class should be static or leaks might occur: IncomingHandler</a> but i have no clue how to fix this in my class. It is a very straightforward class. Create, initialize and start to use it.</p>

<p>I hope someone can help me fix this warning and while we are at it, some hints and tips on my code or comments are very welcome too ;)</p>

<p>MainActivity.java</p>

<pre><code>package com.example.crossfadeimage;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    Xfade xfade = new Xfade();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // INIT(current activity, image id's, time between fades, fade speed)
        xfade.init(this, new int[]{ R.id.image1, R.id.image2, R.id.image3 }, 3000, 500);
        xfade.start();

    }

}
</code></pre>

<p>Xfade.java</p>

<pre><code>package com.example.crossfadeimage;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

public class Xfade {

    private Activity activity;

    // Handler
    private Handler handlerTimer = new Handler();
    private static final int UPDATE_STUFF_ON_DIALOG = 999;
    private int updateTime;

    private Animation fadeIn;
    private Animation fadeOut;

    public int[] xfadeImages;

    public int xfadeCounter = 1;

    public void init(Activity thisActivity, int[] images, int time,
            int animationSpeed) {

        activity = thisActivity;
        xfadeImages = images;
        updateTime = time;

        // Set Animations
        fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setDuration(animationSpeed);

        fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setDuration(animationSpeed);

        // Hide all images except the first
        // which is always visible
        for (int image = 1; image &lt; xfadeImages.length; image++) {

            ImageView thisImage = (ImageView) activity
                    .findViewById(xfadeImages[image]);
            thisImage.setVisibility(4);

        }

    }

    public void start() {

        handlerTimer.removeCallbacks(taskUpdateStuffOnDialog);
        handlerTimer.postDelayed(taskUpdateStuffOnDialog, updateTime);

    }

    private Runnable taskUpdateStuffOnDialog = new Runnable() {
        public void run() {

            Message msg = new Message();
            msg.what = UPDATE_STUFF_ON_DIALOG;
            handlerEvent.sendMessage(msg);

            // Repeat this after 'updateTime'
            handlerTimer.postDelayed(this, updateTime);
        }
    };

    private Handler handlerEvent = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_STUFF_ON_DIALOG: {
                crossFade();
            }
                break;
            default: {
                super.handleMessage(msg);
            }
                break;
            }
        }
    };

    public void crossFade() {

        if (xfadeCounter == 0) {
            ImageView lastImage = (ImageView) activity
                    .findViewById(xfadeImages[xfadeImages.length - 1]);
            lastImage.setVisibility(4);
        }

        if (xfadeCounter &lt; xfadeImages.length) {

            ImageView thisImage = (ImageView) activity
                    .findViewById(xfadeImages[xfadeCounter]);
            thisImage.setVisibility(0);
            thisImage.startAnimation(fadeIn);

            xfadeCounter++;

        } else {

            // Hide all images except the first
            // before fading out the last image
            for (int image = 1; image &lt; xfadeImages.length; image++) {

                ImageView thisImage = (ImageView) activity
                        .findViewById(xfadeImages[image]);
                thisImage.setVisibility(4);

            }

            // Fadeout
            ImageView lastImage = (ImageView) activity
                    .findViewById(xfadeImages[xfadeImages.length - 1]);
            lastImage.startAnimation(fadeOut);

            // LastImage is faded to alpha 0 so it doesn't have to be hidden
            // anymore
            xfadeCounter = 1;

        }

    }

}
</code></pre>
