Android 定时器处理程序

发布于 2024-09-17 21:11:00 字数 1339 浏览 2 评论 0原文

我正在使用这段代码:

public class newtimer extends Activity {

    private Timer myTimer;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
            }

        }, 0, 1000);
    }

    int number = 0;

    private void TimerMethod()
    {
        //This method is called directly by the timer
        //and runs in the same thread as the timer.

        //We call the method that will work with the UI
        //through the runOnUiThread method.

        Toast.makeText(this, "TimerMethod Running "+number, Toast.LENGTH_SHORT).show();

        this.runOnUiThread(Timer_Tick);
    }

    private Runnable Timer_Tick = new Runnable() {
        public void run() {

            //This method runs in the same thread as the UI.
            //Do something to the UI thread here

            Toast.makeText(getBaseContext(), "UI Thread Running "+number, Toast.LENGTH_SHORT).show();

        }
    };
}

当我运行它时,我在 logcat 中收到此异常:

09-06 21:39:39.701: ERROR/AndroidRuntime(1433): java.lang.RuntimeException: 无法在未调用 Looper.prepare() 的线程内创建处理程序

I am using this code:

public class newtimer extends Activity {

    private Timer myTimer;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
            }

        }, 0, 1000);
    }

    int number = 0;

    private void TimerMethod()
    {
        //This method is called directly by the timer
        //and runs in the same thread as the timer.

        //We call the method that will work with the UI
        //through the runOnUiThread method.

        Toast.makeText(this, "TimerMethod Running "+number, Toast.LENGTH_SHORT).show();

        this.runOnUiThread(Timer_Tick);
    }

    private Runnable Timer_Tick = new Runnable() {
        public void run() {

            //This method runs in the same thread as the UI.
            //Do something to the UI thread here

            Toast.makeText(getBaseContext(), "UI Thread Running "+number, Toast.LENGTH_SHORT).show();

        }
    };
}

When i run it i get this exception in logcat:

09-06 21:39:39.701: ERROR/AndroidRuntime(1433): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-09-24 21:11:00

我认为问题出在 TimerMethod 函数中的 Toast.makeText(this, "TimerMethod Running "+number, Toast.LENGTH_SHORT).show(); - 您无法调用任何相关函数从工作线程到 UI。既然在UI线程中运行的部分已经有一个Toast了,为什么在TimerMethod中还要再有一个呢?

对于调试,我建议尽可能使用 Log 而不是 Toast。

I would assume the problem is with the Toast.makeText(this, "TimerMethod Running "+number, Toast.LENGTH_SHORT).show(); in your TimerMethod function - you can't call any functions pertaining to the UI from worker threads. Since you already have a Toast in the portion that runs in the UI thread, why do you have another one in TimerMethod?

For debugging, I would recommend using Log as much as possible instead of Toast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文