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)
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.
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 .
It seems you have imported a wrong Handler class
import java.util.logging.Handler;
Change it to
import android.os.Handler;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With