Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler is abstract ,cannot be instantiated

I am trying to use a Handler in my app. However, 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(,int)

like image 679
Chinmay Dabke Avatar asked Nov 09 '13 06:11

Chinmay Dabke


People also ask

Is abstract Cannot be instantiated Android studio?

Yes, the answer is still the same, the abstract class can't be instantiated, here in the second example object of ClassOne is not created but the instance of an Anonymous Subclass of the abstract class.

Is abstract Cannot be initialized?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .


1 Answers

It seems you have imported a wrong Handler class

import java.util.logging.Handler; 

Change it to

import android.os.Handler; 
like image 155
Glenn Avatar answered Sep 30 '22 05:09

Glenn