Recently write a small project yourself practiced hand, create a thread to fetch data from the network and then display it on the Recyclerview. The page can be displayed when it is written, but sometimes the requested data is displayed, sometimes not. Click on Android Monitor to see that there is a hint:
Only the original thread, created a view hierarchy can touch its views.
The exception means that only the thread that created the view can manipulate the view, and it is common to assume that the view is created in a non-UI thread before this error occurs.
Originally I would like to be under, can see forget, I will simply use fragment. But as programmers we must ask the bottom of the root.
The data code for this request is as follows:
NewThread (NewRunnable () {@Override Public voidrun () {Try{String URL= ""; Okhttpclient Client=Newokhttpclient (); Request Request=NewRequest.builder (). URL (URL). Method ("GET",NULL). build (); OKHTTP3. Response Response=Client.newcall (Request). Execute (); if(Response.issuccessful ()) {String responsestring= (Response.body () = =NULL? "": Response.body (). String ()); ...... Parsing Data Widgetactionevent Event=Newwidgetactionevent (Widgetactionevent.action_click); Event.object=Feedmodel; Eventbus.getdefault (). Post (event); //Iloaddata.loaddata (Feedmodel); } Else{log.i (TAG,"OkHttp is request Error"); } } Catch(IOException e) {e.printstacktrace (); }}). Start ();
After the data request, parse, and use Eventbus to transfer the data.
PS: If you call the above code in mainactivity, it will not produce an exception, because all are running in the main thread.
Variant One: Crash
So I changed a form to pass the data, this time using the callback method, which is commented out above the line of code:
Iloaddata.loaddata (Feedmodel);
This time without eventbus unexpectedly collapsed ... What can I do, I also very helpless ah.
FATAL exception:thread-9324916android.view.viewrootimpl$calledfromwrongthreadexception:only the Original thread that created a view hierarchy can touch it views.
Workaround: Adopt handle
The implementation code is as follows:
/*** Initiating a network request*/ Public Static voidOkhttp_synchronousget (FinalHandler Handler) { NewThread (NewRunnable () {@Override Public voidrun () {Try{String URL= "Http://eff.baidu.com:8086/action/combined/action_RetData.php?name=shenjiaqi"; Okhttpclient Client=Newokhttpclient (); Request Request=NewRequest.builder (). URL (URL). Method ("GET",NULL). build (); OKHTTP3. Response Response=Client.newcall (Request). Execute (); if(Response.issuccessful ()) {String responsestring= (Response.body () = =NULL? "": Response.body (). String ()); ...... Parsing Data handler.sendmessage (Handler.obtainmessage (22, Feedmodel)); } Else{log.i (TAG,"OkHttp is request Error"); } } Catch(IOException e) {e.printstacktrace (); }}). Start (); }
Then fragment add the following code to handle the data passed in:
/** * Receive parsed data sent back/new Handler () { @ Override publicvoid handlemessage (Message msg) { = (Object) Msg.obj; showpictures (model); } };
This will be the perfect solution to the problem.
Android:only the original thread that created a view hierarchy can touch it views exception