Handler detail series (6) -- View post () method details, handlerpost
MainActivity is as follows:
Package cc. testui2; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. app. activity;/*** Demo Description: * method for changing the UI in a child thread ** the post () method of the View is used in the child thread. * Based on the source code, we can see whether it finally calls the post () method of the handler of the UI thread. * do not perform time-consuming operations in the post method ** check the source code of this method: ** Causes the Runnable to be added to the message queue. * The runnable Will be run on the user interface thread. ** @ param action The Runnable that will be executed. ** @ return Returns true if the Runnable was successfully placed in to the * message queue. returns false on failure, usually because the * logoff processing the message queue is exiting. ** public boolean post (Runnable action) {* final AttachInfo attachInfo = mAttachInfo; * if (attachInfo! = Null) {* return attachInfo. mHandler. post (action); *} * // Assume that post will succeed later * ViewRootImpl. getRunQueue (). post (action); * return true; *} *** English comment of this method: * put Runnable into the message queue and execute it in the UI thread. ** reference: * http://blog.csdn.net/guolin_blog/article/details/9991569 * Thank you very much */public class MainActivity extends Activity {private TextView mTextView; private TextView mTipTextView; private Button mButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mTextView = (TextView) findViewById (R. id. textView); mTipTextView = (TextView) findViewById (R. id. tipTextView); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new OnClickListenerImpl (); System. out. println ("UI Thread ID =" + Thread. currentThread (). getId ();} private class OnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {mTipTextView. post (new Runnable () {@ Overridepublic void run () {mTextView. setText ("My number is 9527"); System. out. println ("view. post (new Runnable () in the run () method Thread ID = "+ Thread. currentThread (). getId ());}});}}}
Main. xml is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: id = "@ + id/textView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "test" android: layout_centerHorizontal = "true" android: layout_marginTop = "50dip"/> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button" android: layout_centerHorizontal = "true" android: layout_marginTop = "120dip"/> <TextView android: id = "@ + id/tipTextView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Update UI in child thread" android: layout_centerInParent = "true"/> </RelativeLayout>
Handlerpost (); and viewpost () in android; answers to experienced developers
Consistent core principles
You can create your own front, and you can complete it with the view inside.
Post method for Android Handler
This Handler class exists in Android, but you have defined a Handler class here, so you use your own Handler class, so the above error will be reported.