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