Thoughts on updating the UI of Handler, Thread, and Runnable in android

Source: Internet
Author: User

When I was writing a small application, I encountered a question about Handler, Thread, and runnable. I asked all the people in the group that couldn't solve it. Then I checked a lot of information and finally got to know something, now I will share it with you.

It is mainly about the difference between Thread and Runnable, and the impact they have when using Handler ~, I first go to the code

The following is the error code ~ If the following code is executed in handler to update the UI, a null pointer error will be reported ~ Thread used at the Thread

public class MainActivity extends Activity {TextView text1, text2;Button button;myThread th;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text1 = (TextView)findViewById(R.id.text1);text2 = (TextView)findViewById(R.id.text2);button = (Button)findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubth = new myThread();th.start();}});}Handler myHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case 1:System.out.println("-----------1--------------");System.out.println(msg.getData().getString("s1"));System.out.println(msg.getData().getString("s2"));try{ text1.setText(msg.getData().getString("s1")); text2.setText(msg.getData().getString("s1"));}catch(Exception e){e.printStackTrace();}break;default:break;}super.handleMessage(msg);}};}class myThread extends Thread{String s1, s2;MainActivity ma = new MainActivity();public void run(){  s1 = "fsdfsgfdsgdfgfdgdhshshs";  s2 = "fsfsdgdshdhdshrehreherh";    Message msg = new Message();  msg.what = 1;  Bundle bundle = new Bundle();  bundle.putString("s1", s1);  bundle.putString("s2", s2);  msg.setData(bundle);  ma.myHandler.sendMessage(msg);}}

The above code is not commented, but it is very simple. I believe everyone can understand it ~, This code is incorrect.

The correct code is provided below ~ Runnable used at the thread

public class MainActivity extends Activity {TextView text1, text2;Button button;Thread th;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text1 = (TextView)findViewById(R.id.text1);text2 = (TextView)findViewById(R.id.text2);button = (Button)findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubth = new Thread(runnable);th.start();}});}Handler myHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case 1:System.out.println("-----------1--------------");System.out.println(msg.getData().getString("s1"));System.out.println(msg.getData().getString("s2"));try{ text1.setText(msg.getData().getString("s1")); text2.setText(msg.getData().getString("s1"));}catch(Exception e){e.printStackTrace();}break;default:break;}super.handleMessage(msg);}};Runnable runnable = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub      String s1 = "fsdfsgfdsgdfgfdgdhshshs";  String s2 = "fsfsdgdshdhdshrehreherh";  Message msg = new Message();  msg.what = 1;  Bundle bundle = new Bundle();  bundle.putString("s1", s1);  bundle.putString("s2", s2);  msg.setData(bundle);  MainActivity.this.myHandler.sendMessage(msg);}};}

This is the correct code and the UI can be correctly updated.


After finding the problem, I did a few more experiments and found that Thread can also be used to update the UI, just like this.

public class MainActivity extends Activity {TextView text1, text2;Button button;myThread th;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text1 = (TextView)findViewById(R.id.text1);text2 = (TextView)findViewById(R.id.text2);button = (Button)findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubth = new myThread();th.start();}});}Handler myHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case 1:System.out.println("-----------1--------------");System.out.println(msg.getData().getString("s1"));System.out.println(msg.getData().getString("s2"));try{ text1.setText(msg.getData().getString("s1")); text2.setText(msg.getData().getString("s1"));}catch(Exception e){e.printStackTrace();}break;default:break;}super.handleMessage(msg);}};class myThread extends Thread{public void run() {// TODO Auto-generated method stub      String s1 = "fsdfsgfdsgdfgfdgdhshshs";  String s2 = "fsfsdgdshdhdshrehreherh";  Message msg = new Message();  msg.what = 1;  Bundle bundle = new Bundle();  bundle.putString("s1", s1);  bundle.putString("s2", s2);  msg.setData(bundle);  MainActivity.this.myHandler.sendMessage(msg);}}}


I don't know where the problem is found?

As far as I know ~ The differences between Thread and Runnable are as follows:

For example, if a thread implements the Runnable interface, when calling this thread object to open up multiple threads, these threads can call the same variable ~, (To put it bluntly, both the main thread and the Child thread thrown can use the variables of your main thread)

If the Thread class is inherited from the Thread class, the above functions are implemented through the internal class, the internal class can be used to access the external class at Will (This section describes the last segment of my three code segments ).

Okay, that's all. I hope it will help you.


Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.