97

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)

6 Answers 6

398

It seems you have imported a wrong Handler class

import java.util.logging.Handler;

Change it to

import android.os.Handler;
4
  • 3
    It certainly was helpful to me - as an iOS developer coming back to occasional projects for Android, Android Studio has some annoying quirks, trying to be TOO clever at importing/autocompleting is one i.e. is quicker 90% of the time, is 5 x slower 10% of the time. Thanks again Mar 12, 2014 at 22:20
  • 1
    @Glenn--, srsly, this just solved all manner of other errors in my code. ++ to this answer! Mar 12, 2014 at 22:28
  • What is difference between both of them? Oct 16, 2015 at 7:48
  • It's hard to believe they would use a classname that's already defined in java...
    – Julius
    Feb 23, 2018 at 23:50
5

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()){....`

2

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

1
  • This is completely ridiculous on android's part
    – Julius
    Feb 23, 2018 at 23:51
1
    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();
        };
    }
}
1
  • 2
    Care to add some explanation?
    – Onik
    Nov 13, 2015 at 9:44
1

It seems like you have implemented the wrong Handler class

import java.util.logging.Handler;

Change it to

import android.os.Handler;

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.