处理器是抽象的,不能被实例化抽象、处理器、实例

2023-09-05 05:48:03 作者:爱是夏诺

我试图用一个处理程序在我的应用程序。但是,当我实例化这样的:

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)

请帮忙! 先谢谢了。

推荐答案

看来你已经导入了错误的处理程序类

It seems you have imported a wrong Handler class

import java.util.logging.Handler;

将其更改为

import android.os.Handler;