Android:only the original thread that created a view hierarchy can touch it views exception

Source: Internet
Author: User
Tags eventbus

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

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.