Java RunOnUiThread未更新文本视图

Java RunOnUiThread未更新文本视图,java,android,multithreading,Java,Android,Multithreading,我想写一个实时计数器。我尝试使用线程,但android警告我,只有活动线程可以接触视图。我找到了一个使用runOnUiThread的解决方案,但它不太管用 public class CounterInRealTimeExampleActivity extends Activity { private TextView textView; /** Called when the activity is first created. */ @O

我想写一个实时计数器。我尝试使用线程,但android警告我,只有活动线程可以接触视图。我找到了一个使用runOnUiThread的解决方案,但它不太管用

    public class CounterInRealTimeExampleActivity extends Activity {
        private TextView textView;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LinearLayout layout = new LinearLayout(this);
            textView = new TextView(this);
            textView.setTag(new Integer(0));

            layout.addView(textView);
            setContentView(layout);
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    //while(true)
                    {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        increment();
                    }
                }
            });
        }
        public void increment() {
            textView.setTag(new Integer((Integer)textView.getTag())+1);
            textView.setText(Integer.toString((Integer)textView.getTag()));
        }
    }

我在这里粘贴代码。愿这对你有帮助

class UpdateTimeTask extends TimerTask {    

    public void run() {

        String time =  DateUtils.now("hh:mm:ss'T'a");              
        String[]arrValues = time.split("T");
        if(arrValues.length>0) {
            String strValue= arrValues[0];
            String []arrTimeValues = strValue.split(":");  
            String strValue1= arrTimeValues[2];
            setTimertext(strValue1);                   
        }                      
    }

    public void setTimertext(String strValue) {         
        runOnUiThread(new Runnable() {
            public void run() {
                FinalTime=timer_time--;                 
                btnTimer.setText(String.valueOf(FinalTime));    
            }
        });
    }

}

我在这里粘贴代码。愿这对你有帮助

class UpdateTimeTask extends TimerTask {    

    public void run() {

        String time =  DateUtils.now("hh:mm:ss'T'a");              
        String[]arrValues = time.split("T");
        if(arrValues.length>0) {
            String strValue= arrValues[0];
            String []arrTimeValues = strValue.split(":");  
            String strValue1= arrTimeValues[2];
            setTimertext(strValue1);                   
        }                      
    }

    public void setTimertext(String strValue) {         
        runOnUiThread(new Runnable() {
            public void run() {
                FinalTime=timer_time--;                 
                btnTimer.setText(String.valueOf(FinalTime));    
            }
        });
    }

}
这绝对是个问题。不允许您阻止UI线程,这正是
sleep
所做的。您必须单独执行代码才能从
runOnUiThread
中更新UI


这绝对是个问题。不允许您阻止UI线程,这正是
sleep
所做的。您必须执行代码,以从
runOnUiThread
中更新UI,仅此

在Hasmukh提示后找到解决方案

package com.android.examples;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CounterInRealTimeExampleActivity extends Activity {
    private TextView textView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        textView = new TextView(this);
        layout.addView(textView);
        setContentView(layout);

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {

                    updateTime();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    private void updateTime() {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                textView.setText(Integer.toString((int) (Calendar.getInstance()
                        .getTimeInMillis() / 1000) % 60));
            }
        });
    }
}

工作一,哈斯穆克提示后找到了解决方案

package com.android.examples;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CounterInRealTimeExampleActivity extends Activity {
    private TextView textView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        textView = new TextView(this);
        layout.addView(textView);
        setContentView(layout);

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {

                    updateTime();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    private void updateTime() {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                textView.setText(Integer.toString((int) (Calendar.getInstance()
                        .getTimeInMillis() / 1000) % 60));
            }
        });
    }
}

你试过了吗?你试过了吗?非常感谢(我不能投票,因为我的声誉很低,但我会的;pokey没问题,声誉并不重要,但如果这个解决方案对你有帮助,我真的很高兴。非常感谢你(我不能投票,因为我的声誉很低,但我会的;pokey没问题,声誉并不重要,但如果这个解决方案对你有帮助,我真的很高兴。谢谢