--begin by itlife365
Handler is abstract;can not be instantiated android
编写如下代码的时候
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
我点击自动提示Tip的解决方案时,自动提示了如下方法,
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) {
}
};
因为之前没有使用过Hnadlers ,不知道这个是怎么回事。我只是想实现延时的效果而已,从示例代码执行的如下代码
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);
baidu 后,找到原因了,导入错包了。I have imported a wrong Handler class
import java.util.logging.Handler;
Change it to,删除后改为如下代码解决了,没有在报错
import android.os.Handler;
why-Handler-can-not-be-instantiated-android
--end by itlife365