3

我是 Android 新手,我正在尝试完成一个关于将 aProgressBarHandler. 请注意,我的目标是 API 11。

这是我的代码,其中包含我正在内联的错误:

public class HandlerDemo extends Activity {

    ProgressBar bar;
    Handler handler = new Handler() {

        @Override
        public void handleMessage() {   // Shows "method must override or implement supertype method" on this line.
            bar.incrementProgressBy(5);
        }

        @Override
        public void close() {

        }

        @Override
        public void flush() {

        }

        @Override
        public void publish(LogRecord arg0) {

        }
    };

    AtomicBoolean isRunning = new AtomicBoolean(false);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_demo);
        bar = (ProgressBar)findViewById(android.R.id.progress);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_handler_demo, menu);
        return true;
    }

    public void onStart() {
        super.onStart();
        bar.setProgress(0);

        Thread background = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for(int i = 0; i < 20 && isRunning.get(); i++) {
                        Thread.sleep(1000);
                        handler.sendMessage(handler.obtainMessage()); // Shows "Method is undefined for type Handler" on this line.
                    }
                } catch(Throwable t) { }
            }
        });
        isRunning.set(true);
        background.start();
    }

    public void onStop() {
        super.onStop();
        isRunning.set(false);
    }
}

为什么我会收到这些错误?

4

1 回答 1

3

您的Handler导入错误。它应该是android.os.Handler

于 2013-03-14T17:31:03.157 回答