This article describes the Android implementation method to update the UI in the Activity in child threads. Share it for your reference, as follows:
On the Android platform, when performing multi-threading programming, it is often necessary to perform some processing in a separate thread outside the main thread, and then update the user interface display. However, the problem of directly updating the page display in threads outside the main thread is: the system will report this exception:
ERROR/AndroidRuntime(1222): $CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Perhaps the programmer will call the() method in the thread to try to display some prompt information in the UI, which will also report the following error:
Can't create handler inside thread that has not called ()
Solution: The child thread cannot directly update the UI in the Activity. The general method is that the child thread passes messages to the Activity, and then the Activity updates the UI itself based on these messages. There is a class in Android called "It's used to do this."
1. Declare a class variable in the activity that needs to be updated by the UI by thread.
private Handler handler;
2. Add handler initialization in the onCreate function:
@Override public void onCreate(Bundle savedInstanceState) { //Other codes...//…… //…… handler=new Handler(){ public void handleMessage(Message msg){ String message=(String);//obj does not necessarily have to be a String class, it can be another class, depending on the user's specific application //Change the main thread's UI according to the information in the message //…… } } };
In addition, the Activity needs to provide the handler's get function so that the thread can get the handler and then pass the message.
public Handler getHandler(){ return ; }
3. The child thread class needs to hold a Context class object that represents the context. In actual application, this reference points to the Activity object to update the UI. It is generally declared as:
private Context ctx;
Then initialize ctx in the child thread class constructor or other functions. This step is to obtain the Handler object in the Activity object. (Or it's OK to use other methods, as long as the child thread can get the handler object in the Activity.)
4. In the last step, when the child thread runs to a certain place and needs to pass a message to the Activity, create a class object, add the object to be transmitted to the message, and publish it through Handler to send it to the main thread. The code example is as follows:
String str_temp="Message to be passed to the main thread" Message message = (); =str_temp; //Posted the transmission message through the Handler, the handler(message);
Remember, the handler here is the same object as the handler in the Activity, so that the message is sent to that Activity.
In addition, this method not only allows the child thread to update the UI, but also has other uses. Now we assume that the child thread may throw some errors, which should be normal. So how can the error message be made known to the user? It's very simple. In the catch statement block, put the caught error object and pass it to the Activity. The error message is displayed using the () method in the Activity.
For more information about Android related content, please check out the topic of this site:Summary of the usage of Android threads and message mechanisms》、《Android programming activity operation skills summary》、《Android debugging skills and solutions to common problems》、《Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.