创建视图层次结构的原始线程可以触及其视图

时间:2014-05-06 06:09:12

标签: android timer

如果来自DB的特定计数大于0并尝试将计数值设置为textView,我将尝试启动动画。我有一个计时器来检查每一秒的表计数是否大于0

在Oncreate中调用timeMethod

private void timerMethod()
{
    //Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() 
    {
        @Override
        public void run() 
        {
            updatescreen();
        }

    }, 0, 1000);
}

private void updatescreen() 
{
    DB = new Databasehandler(this);
    int counter = DB.getCount();

    if (counter > 0)
    {
        Log.e("DB","Counter" +counter);

        UnreadCount = DB.getCount();
        count.setText(""+UnreadCount);

        if(AnimStarted.equalsIgnoreCase("YES"))
        { 
            image.startAnimation(anim);
            AnimStarted = "NO";
        }
    }

但是通过这样做我有以下错误:

05-06 11:32:46.663: E/AndroidRuntime(24700): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.checkForRelayout(TextView.java:6600)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.setText(TextView.java:3813)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.setText(TextView.java:3671)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.setText(TextView.java:3646)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at com.jay.te.MainScreen.updatescreen(MainScreen.java:2970)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at com.jay.te.MainScreen.access$31(MainScreen.java:2960)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at com.jay.te.MainScreen$10.run(MainScreen.java:2954)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at java.util.Timer$TimerImpl.run(Timer.java:284)

不确定这里究竟是什么问题?有人可以帮我解决这个问题吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您无法从非ui线程更新UI。您有一个在后台线程上运行的计时器,您可以在updatescreen更新UI。

使用runOnUiThread并更新ui或延迟使用Handler

  runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           // update ui here
                        }
                 });