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