简体   繁体   English

从 TimerTask 的运行方法更新 TextView 时出错

[英]Error updating TextView from TimerTask's run method

I'm trying to test UI and Timer class possibilities.我正在尝试测试 UI 和计时器 class 的可能性。 So I tried the following exercise:所以我尝试了以下练习:

=== TestTimerActivity.java === === TestTimerActivity.java ===

package com.tvt.TestTimer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;


public class TestTimerActivity extends Activity {
    TextView _tv;
    Timer _t;
    int _count = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //

//        _tv = (TextView) getWindow().getDecorView().findViewById( R.id.TextViewTime );

        _tv = (TextView) findViewById( R.id.TextViewTime );

        UpdateTime(); // Completes OK

        _t = new Timer();

        _t.scheduleAtFixedRate( new TimerTask() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                _count++;
                UpdateTime(); // Fails
            }
        }, 1000, 1000 );
    }

    protected void UpdateTime() {
        _tv.setText( "" + _count );
    }
}

=== TestTimerActivity.java === === TestTimerActivity.java ===

=== main.xml === === main.xml ===

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/header" android:id="@+id/TestViewHeader" android:textStyle="bold"/>
<TextView android:layout_height="wrap_content" android:id="@+id/TextViewTime" android:layout_width="match_parent" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:text="-"></TextView>
</LinearLayout>

=== main.xml === === main.xml ===

The very first UpdateTime call (from onCreate) completes OK but the same call from TimerTask::run() fails giving the message "TestTimer class has stopped unexpectedly".第一个 UpdateTime 调用(来自 onCreate)正常完成,但来自 TimerTask::run() 的相同调用失败,显示消息“TestTimer class 已意外停止”。

Any idea?任何想法? Where is my fault?我的错在哪里?

-- --
SY, TVT SY, TVT

Do that UpateTime() operation on the UI thread.在 UI 线程上执行该 UpateTime() 操作。

protected void UpdateTime() 
{
     runOnUiThread(new Runnable() 
     {
    public void run() 
    {
            _tv.setText( "" + _count );
        }
     });
}

Hopefully resolves your problem.希望能解决您的问题。

Updating the UI from a thread other than the UI/main thread will fail as Android does not allow it.从 UI/主线程以外的线程更新 UI 将失败,因为 Android 不允许这样做。 Try using a Handler to post messages back to the UI thread.尝试使用处理程序将消息发布回 UI 线程。

Or maybe try using AsyncTask which presents a set of really simple callback methods to handle the worker and UI threads.或者可以尝试使用AsyncTask ,它提供了一组非常简单的回调方法来处理工作线程和 UI 线程。 But that might not be exactly what you need since you're trying to test out the TimerTask class.但这可能不是您所需要的,因为您正在尝试测试 TimerTask class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM