Only the original thread that created a view hierarchy can touch it views exception

Source: Internet
Author: User

Write code when you touch android.view.viewrootimpl$calledfromwrongthreadexception:only the original thread that created a view Hierarchy can touch its views. This exception.
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.
But this error also occurs in my code when I create the view in the UI thread.

Here is the code I went wrong with:

Asynctask<void, void, void> mytask = new asynctask<void, void, void> () {    ProgressDialog progressdialog;@ overrideprotected void OnPreExecute () {super.onpreexecute ();p rogressdialog = new ProgressDialog (GetContext ()); Progressdialog.setprogressstyle (Progressdialog.style_spinner);p rogressdialog.setmessage ("Please later ..."); Progressdialog.show ();} @Overrideprotected void Doinbackground (void ... params) {//time-consuming operation ... return null;} @Overrideprotected void OnPostExecute (void result) {Super.onpostexecute (Result);p Rogressdialog.dismiss ();}    ;    Mytask.execute ();

As above, the above error will also occur when creating ProgressDialog in the UI process.

Finally, in a close examination, it was found that View.invalidate () was called in the Doinbackground method.

Calling this method invokes the view's OnDraw () method, which causes the window to redraw, and the original UI process is destroyed. Wait until the doinbackground is executed, then call OnPostExecute this method to execute Progressdialog.dismiss (), the UI process that originally created it was not found. Naturally it will be reported android.view.viewrootimpl$calledfromwrongthreadexception:only the original thread that created a view hierarchy can touch its views. This is a mistake.

In addition, the function that normally causes the invalidate () operation is as follows:

1. Call the Invalidate () method directly, request a redraw (), but only draw the caller itself.

2. SetSelection () Method: Request Redraw (), but only the caller itself is drawn.

3. Setvisibility () Method: The Invalidate () method is called indirectly when the view visual state transitions to the visible invisible.

The view is then drawn.

4. SetEnabled () method: Requests a redraw (), but does not redraw any views including the caller itself.

When you look at the information about this exception, you find that another condition can also cause this exception:

http://my.oschina.net/qixiaobo025/blog/195396

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.