Android,如何从私有内部类(AsyncTask)更新视图元素

时间:2014-01-23 18:25:00

标签: java android android-asynctask

我正在制作一个Android应用程序作为学校作业的一部分。

我需要能够在hh:mm:ss中显示一个计时器并每秒更新一次计时器。

我已经设法提出了我认为会产生我需要的结果的代码,但我无法让AsyncTask每秒执行一次。这是我目前的代码:

TimerHand timerHand = new TimerHandling(timerTextView);
Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                timerHand.execute();

            } },1000, 1000); // 1000ms == 1 second

这是Async任务,这是一个包含上述代码的类中的嵌套类,以及TextView tv的声明:

   class TimerHandling  extends AsyncTask<Void, Void, Void>{

        private long secs, mins, hours, millis;
        private String timeString;
        private TextView tv;
        private long startTime;
        private boolean stop;
        private Handler handler;

        public TimerHandling(TextView tv){
            this.tv = tv;
            startTime = System.currentTimeMillis();
            stop = false;
            handler = new Handler();

        }

        @Override
        protected Void doInBackground(Void... params) {
            while(stop != true){
                millis = System.currentTimeMillis() - startTime;
                hours += TimeUnit.MILLISECONDS.toHours(millis);
                mins +=  TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));
                secs += TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MILLISECONDS.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));     

                timeString = String.format("%02d:%02d:%02d",hours, mins, secs);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText(timeString);
                    }
                });
            }
            return null;
        }

        public void setStopCondition(boolean condition){

            this.stop = condition;
        }
    }
        }

它产生以下logCat输出:

01-23 18:00:27.865: E/AndroidRuntime(14458): FATAL EXCEPTION: AsyncTask #5
01-23 18:00:27.865: E/AndroidRuntime(14458): Process: dcs.aber.ac.uk.cs211.group02, PID: 14458
01-23 18:00:27.865: E/AndroidRuntime(14458): java.lang.RuntimeException: An error occured while executing doInBackground()
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at java.lang.Thread.run(Thread.java:841)
01-23 18:00:27.865: E/AndroidRuntime(14458): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.View.requestLayout(View.java:16431)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.View.requestLayout(View.java:16431)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.View.requestLayout(View.java:16431)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.View.requestLayout(View.java:16431)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.view.View.requestLayout(View.java:16431)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.widget.TextView.checkForRelayout(TextView.java:6600)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.widget.TextView.setText(TextView.java:3813)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.widget.TextView.setText(TextView.java:3671)
01-23 18:00:27.865: E/AndroidRuntime(14458):    at android.widget.TextView.setText(TextView.java:3646)

据我所知,这是因为嵌套类有一个不同的View因此它无法更新TextView,我该如何克服这个?

当我运行我的代码时,我可以看到它正在执行,大约2秒后,应用程序将在发布的错误消息时崩溃。

更新

public void startCountingTimer(){

final Handler handler = new Handler();
final Runnable task = new Runnable() 

{ 
    @Override
    public void run() {

        millis = System.currentTimeMillis() - startTime;
        hours += TimeUnit.MILLISECONDS.toHours(millis);
        mins +=  TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));
        secs += TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MILLISECONDS.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));     

        timeString = String.format("%02d:%02d:%02d",hours, mins, secs);
        handler.postDelayed(task, 1000); //problem on this line
    }
};
task.run();   

}

我已用单行注释指出了问题,“任务”未声明。

1 个答案:

答案 0 :(得分:1)

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

您正在尝试从非ui线程更新ui,这是不可能的。你应该从ui线程更新ui。

Timer在不同的主题上运行。

同时阅读线程规则@ http://developer.android.com/reference/android/os/AsyncTask.html

 I need to be able to display a timer in hh:mm:ss and update the timer every second.

您可以使用HandlerCountDownTimer

Handler与创建它的线程相关联。

http://developer.android.com/reference/android/os/Handler.html

Handler m_handler;
Runnable m_handlerTask ; 
m_handler = new Handler(); 
m_handlerTask = new Runnable() 
{ 
@Override
public void run() {
   // do something
  m_handler.postDelayed(m_handlerTask, 1000); 
 }
 };
 m_handlerTask.run();   

倒数计时器

Countdowntimer in minutes and seconds

编辑:

public class MainActivity extends Activity {

      Handler handler;
      Runnable task ;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startCountingTimer();

}
      public void startCountingTimer() {

       handler = new Handler();
       task = new Runnable() 
          { 
              @Override
              public void run() {
                      // do something
                  handler.postDelayed(task, 1000); 
              }
          };
          task.run();   
          }  
}