我正在尝试在我的应用程序上显示一个计时器,其中包含一个计算已用时间的线程和一个修改视图的处理程序。
我知道android不允许ui线程修改视图的其他线程,所以我使用处理程序来完成这项工作。
但它似乎并不有效,因为我仍然有“android.view.viewroot$CalledFromWrongThreadException:only the original thread that created a view hierarchy can touch its views.”
我该怎么办?
(哦,如果这有帮助的话,代码如下)

package com.hangin.arround.emergency;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;

public class MainActivity extends SherlockActivity {

    private static final String TAG = "MainActivity";

    private TextView elapsedTime;

    private Button startButton;
    private Button stopButton;

    private boolean shouldContinue = false;
    private Handler mHandler;
    private Runnable runnable;

    private static final int MESSAGE_ELAPSED_TIME = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Creating the action bar, initializing my views and stuff

        // Creating the Handler
        mHandler = new Handler() {
            @SuppressLint("SimpleDateFormat")
            @Override
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                switch(msg.what) {
                    case MESSAGE_ELAPSED_TIME :
                        // Converting from milliseconds to a String
                        DateFormat formatter = new SimpleDateFormat("hh:mm:ss.SSS");
                        Calendar calendar = Calendar.getInstance();


                        calendar.setTimeInMillis(msg.arg1);
                        String elapsedTimeString = formatter.format(calendar.getTime());


                        elapsedTime.setText(elapsedTimeString);
                        break;
                    default:
                        break;
                }
            }
        };

        // Creating the Runnable
        runnable = new Runnable(){

            @Override
            public void run() {
                int startTime, actualTime;
                startTime = (int) System.currentTimeMillis();
                Log.d(TAG, "startTime = " + startTime);

                while(shouldContinue) {
                    // Calcul du temps écoulé
                    actualTime = (int)System.currentTimeMillis() - startTime;
                    Log.d(TAG, "actualTime = " + actualTime);

                    // Creating the message sent to the Handler
                    Message elapsedTimeMessage = new Message();
                    elapsedTimeMessage.what = MESSAGE_ELAPSED_TIME;
                    elapsedTimeMessage.arg1 = (int) actualTime;

                    // Sending the message
                    mHandler.handleMessage(elapsedTimeMessage);
                }
            }

        };


        // Adding the OnClickListeners on startButton and stopButton
        startButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {

                // Launching Thread
                (new Thread(runnable)).start();
                shouldContinue = true;
            }
        });

        stopButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {

                // Stopping the thread
                shouldContinue = false;
            }
        });
    }
}

最佳答案

变化

Message elapsedTimeMessage = new Message();
elapsedTimeMessage.what = MESSAGE_ELAPSED_TIME;
elapsedTimeMessage.arg1 = (int) actualTime;
// Sending the message
mHandler.handleMessage(elapsedTimeMessage);


Message.obtain(mHandler, MESSAGE_ELAPSED_TIME, (int) actualTime, 0).sendToTarget();

从api文档来看,handleMessage似乎是稍后在处理过程中调用的,不应该直接调用。

07-24 09:49
查看更多