android: 无法解析方法 sendMessage(androd.os.message)

android: cannot resolve method sendMessage(androd.os.message)

我在学习处理程序时卡住了。以下是我的简单代码:

public class MainActivity extends AppCompatActivity {

    Thread thread;
    public Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        thread = new Thread(new MyThread());
        thread.start();
        handler=new Handler() {
            @Override
            public void close() {

            }

            @Override
            public void flush() {

            }

            @Override
            public void publish(LogRecord record) {

            }

        };
    }

    class MyThread implements Runnable{

        @Override
        public void run(){
            Message message=Message.obtain();
            for(int i=0;i<10000;i++){
                handler.sendMessage(message);
            }
        }
    }

}

在 MyThread class 中,我试图在处理程序之后键入 sendMessage。但是 android studio 没有显示任何此类 sendMessage 选项。我仍然输入它,然后它显示为红色。并说无法解决方法。

您导入了错误的处理程序class

import java.util.logging.Handler;

sendMessage方法不属于这个class

试试这个 导入 android.os.Handler;

示例:

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;


public class MainActivity extends AppCompatActivity {
    Thread thread;
    Handler handler;
    ProgressBar progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressbar = (ProgressBar) findViewById(R.id.progressbar1);
        thread = new Thread(new MyThread());
        thread.start();
        handler = new Handler() {
            public void handleMessage(Message msg) {
                progressbar.setProgress(msg.arg1);
            };
        };

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    class MyThread implements Runnable {
        @Override
        public void run() {

            Message message = Message.obtain();
            Log.d("message", "" + message);
            for (int i = 0; i < 100; i++) {
                message.arg1 = i;

                handler.sendMessage(message);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}