Handler is abstract ,cannot be instantiated

Chinmay Dabke picture Chinmay Dabke · Nov 9, 2013 · Viewed 55.1k times · Source

I am trying to use a Handler in my app. But when i instantiate it like this:

Handler handler = new Handler();

I get the following error.

Gradle: error: Handler is abstract; cannot be instantiated

And when i check the solutions, it asks me to implement these methods:

Handler handler = new Handler() {
        @Override
        public void close() {

        }

        @Override
        public void flush() {

        }

        @Override
        public void publish(LogRecord record) {

        }
    };

I have never used Handlers before and i am using it just to call a method after some delay. To achieve that, I've used:

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 100ms
            }
        }, 100);

But it shows the error:

Gradle: error: cannot find symbol method postDelayed(<anonymous Runnable>,int)

Please help! Thanks in advance.

Answer

Glenn picture Glenn · Nov 9, 2013

It seems you have imported a wrong Handler class

import java.util.logging.Handler;

Change it to

import android.os.Handler;