Only the original thread, created a view hierarchy can touch its views

Source: Internet
Author: User

Today, the actual project encountered this problem, feel that their code is really a bit of a problem, from the Internet to check the information, basically, it is said can not be time-consuming task in the main thread, but bloggers really did not put the time-consuming task in the main thread, good nonsense not to say, first on the code, according to the code says:

@Override
public void onactivitycreated (Bundle savedinstancestate) {
Super.onactivitycreated (savedinstancestate);
Initview ();
Mdialog = Progressdialog.show (Getactivity (), "Download file", "Downloading ...");
Thread loadthread = new Thread (new Loadthread ());
Loadthread.start ();
}


Class Loadthread implements Runnable {
@Override
public void Run () {
InitData ();
}
}


  private void InitData () {
        HttpClient HttpClient = new Defaulthttpclient ();


        HttpGet request;
        try {
            request = new HttpGet (The new URI (Foshifuwu_api ));
            HttpResponse response = Httpclient.execute (request);
            if (Response.getstatusline (). Getstatuscode () = = () {


  &N Bsp             httpentity entity = response.getentity ();
                if (entity! = null) {
          &N Bsp         String out = entityutils.tostring (entity, "UTF-8");

Buddhistservicesbeans.add (New Buddhistservicesbean (title, Des, Money, Unitid, endDate));
The blogger just started putting this code on this sub-thread, not the UI thread
Buddhistservicesadapter = new Buddhistservicesadapter (getactivity (), Buddhistservicesbeans);
Buddhistservices_listview.setadapter (Buddhistservicesadapter);
}
Isnet = true;


} catch (Jsonexception e) {
E.printstacktrace ();
}
}
} else {
Isnet = false;
}


} catch (URISyntaxException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
Message msg = Message.obtain ();
if (isnet) {
Msg.what = 0;
Handler.sendmessage (msg);
} else {
Msg.what = 1;
Handler.sendmessage (msg);
}


}


Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case 0:
Mdialog.cancel ();
Then the blogger put this in the process of processing the UI thread
Buddhistservicesadapter = new Buddhistservicesadapter (getactivity (), Buddhistservicesbeans);
Buddhistservices_listview.setadapter (Buddhistservicesadapter);
Buddhistservices_listview.setonitemclicklistener (New Bsinfoonitemclicklistener ());
Break
Case 1:
Break
}
}
};


I believe that according to the code of the comments people basically understand what is the reason for it, simply speaking, the network request is a time-consuming thread, but the parsing from the web to the JSON on the ListView is not a time-consuming thread, but the main thread, is the UI update thread, so that the main thread and the UI thread is put together, The result is that the reason, believe how to solve the problem everyone knows, the child thread within the child thread needs to do the task, and the UI thread should be the UI thread, these two can not be put together, hope to help everyone, Bo owner for this depressed an afternoon


Only the original thread, created a view hierarchy can touch its views

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.