android报错:Only the original thread that created a view hierarchy can touch its views.

The error location of the problem is that I wrote a timing message with the handler, and directly wrote the ui update operation in the handleMessage() method. The reason for this error report is generally caused by
directly operating the UI in the sub-thread (eg, setText()) . Because the related view and control operations in Android are not thread-safe, Android prohibits updating the UI in non-UI threads.
ps. The operations referred to here are generally operations that can cause the control to be redrawn (invalidate). Executing UI operations such as setTextColor in the child thread will not report an error

**So, what if the child thread needs to operate the UI? Generally speaking, it can be realized in the following ways. The principle is to operate the UI in the main thread through indirect calls:

  1. Use the View.post method, post a Runnable object, and operate the UI in the run method**
final String text = Thread.currentThread().getId() + "_" + i++;
final TextView txtTime1 = (TextView)mainActivity.findViewById(R.id.txtTime1);
txtTime1.post(new Runnable() {
    @Override
    public void run() {
	txtTime1.setText("[View.post]" + text);
    }
});
  1. Use the Activity.runOnUiThread method, pass in a Runnable object, and operate the UI in the run method
final TextView txtTime2 = (TextView)mainActivity.findViewById(R.id.txtTime2);
mainActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
	txtTime2.setText("[Activity.runOnUiThread]");
    }
});
  1. Create a Handler object in the Activity, pass in a Runnable object through the handle.post method in the child thread, and operate the UI in the run method
final TextView txtTime4 = (TextView)mainActivity.findViewById(R.id.txtTime4);
mainActivity.mHandler.post(new Runnable() {
    @Override
    public void run() {
	txtTime4.setText("[Handler.post]" + text);
    }
});
  1. Create a Handler object in the Activity, and operate the UI for the specified message in the handlerMessage method, and the message is sent by a sub-thread through the handler.sendMessage method
    1) Create a Handler object in the onCreate method of the Activity, and receive Operate the UI after the custom message UPDATE_TIME_ID
mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
	super.handleMessage(msg);
	if (msg.what == UPDATE_TIME_ID) {
		String strTime = msg.getData().getString(UPDATE_TIME_KEY);
		((TextView)findViewById(R.id.txtTime3)).setText(strTime);
	} 
    }
};
    2) 在子线程中通过handler.sendMessage方法发送ID为UPDATE_TIME_ID的消息
Message updateTimeMessage = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString(UPDATE_TIME_KEY, "[Handle.sendMessage]" + text);
updateTimeMessage.what = MainActivity.UPDATE_TIME_ID;
updateTimeMessage.setData(bundle);
mainActivity.mHandler.sendMessage(updateTimeMessage);

Guess you like

Origin blog.csdn.net/ShiXinXin_Harbour/article/details/126366032