Java 处理程序是抽象的,无法实例化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19873063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:57:10  来源:igfitidea点击:

Handler is abstract ,cannot be instantiated

javaandroidandroid-handler

提问by Chinmay Dabke

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

我正在尝试Handler在我的应用程序中使用 a 。但是当我像这样实例化它时:

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 Handlersbefore and i am using it just to call a method after some delay. To achieve that, I've used:

我以前从未使用Handlers过,我只是在延迟一段时间后使用它来调用方法。为了实现这一点,我使用了:

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.

请帮忙!提前致谢。

采纳答案by Glenn

It seems you have imported a wrong Handler class

您似乎导入了错误的 Handler 类

import java.util.logging.Handler;

Change it to

将其更改为

import android.os.Handler;

回答by Vahe Gharibyan

    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

public class ActionActivity extends ActionBarActivity {

    final String LOG_TAG = "myLogs";
    TextView tvInfo;
    Button btnStart;
    Handler h;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.action_activity);
        tvInfo = (TextView)findViewById(R.id.tvinfo);
        btnStart = (Button)findViewById(R.id.btnstart);

        h = new Handler() {
            public void handleMessage(android.os.Message msg) {
                // update TextView
                tvInfo.setText("Закачано файлов: " + msg.what);
                if (msg.what == 10) btnStart.setEnabled(true);
            };
        };

    }

    public void onclick(View v) {
        switch (v.getId()) {
            case R.id.btnstart:
                btnStart.setEnabled(false);
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        for (int i = 1; i <= 10; i++) {
                            // some process
                            downloadFile();
                            h.sendEmptyMessage(i);

                            Log.d(LOG_TAG, "i = " + i);
                        }
                    }
                });
                t.start();
                break;
            case R.id.btnTets:
                Log.d(LOG_TAG, "test");
                break;
            default:
                break;
        }
    }

    public void downloadFile(){
        try{
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e){
                e.printStackTrace();
        };
    }
}

回答by Anand Saurabh

In Place Of

代替

import java.util.logging.Handler; 

add

添加

import android.os.Handler;

also if you use

如果你使用

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

    }

    @Override
    public void flush() {

    }

    @Override
    public void publish(LogRecord record) {

    }
};

it will give error that boolean found somthing like error so either use boolean handler = new Handler()... or simply use (new Handler()){....`

它会给出布尔值发现类似错误的错误,所以要么使用 boolean handler = new Handler()... 要么简单地使用 (new Handler()){....`

回答by Sujith Ks

import android.os.Handler; this the handler needed for your purpous. Before importing the Handler class please try to import the above.

导入 android.os.Handler; 这是您的目的所需的处理程序。在导入 Handler 类之前,请尝试导入以上内容。

回答by SPC700

Android SDK auto imports the incorrect one. That's why people have problems.

Android SDK 自动导入不正确的。这就是为什么人们会遇到问题。