android - How to avoid exception "Only the original thread that created a view hierarchy can touch its views"? -


i using asynctaskfor downloading video file update views inside asynctask exception arises "only original thread created view hierarchy can touch views". here sample code. please check , suggest me need correction.

the exception arises @ these lines.

tvpercent.settext((int)per + "%"); tvspeed.settext(networkspeed+"kbps"); 

asynctask class

public class downloadfilefromurl extends asynctask<string, string, string> {      progressbar progbar;     textview tvspeed;      textview tvpercent;      int downloadedsize = 0;     int totalsize = 0;      private long networkspeed;           private long previoustime;     private long totaltime;  //  public downloadfilefromurl(progressbar pb) //  { //      progbar = pb; //       //  }      public downloadfilefromurl(progressbar pb, textview speed, textview per)     {         progbar = pb;         tvspeed = speed;         tvpercent = per;     }       @override     protected void onpreexecute()      {         super.onpreexecute();          progbar.setprogress(0);         //progbar.setmax(100);          previoustime = system.currenttimemillis();     }       @override     protected string doinbackground(string... videourl)      {         try          {              url url = new url(videourl[0]);             httpurlconnection urlconnection = (httpurlconnection) url.openconnection();              urlconnection.setrequestmethod("get");             urlconnection.setdooutput(true);              //connect             urlconnection.connect();              file file = getoutputmediafile(videourl[0]);              fileoutputstream fileoutput = new fileoutputstream(file);              //stream used reading data internet             inputstream inputstream = urlconnection.getinputstream();              //this total size of file downloading             totalsize = urlconnection.getcontentlength();              log.e("download", ""+totalsize);              progbar.setmax(totalsize);               //create buffer...             byte[] buffer = new byte[1024];             int bufferlength = 0;              while ( (bufferlength = inputstream.read(buffer)) > 0 )              {                 fileoutput.write(buffer, 0, bufferlength);                 downloadedsize += bufferlength;                    // update progressbar //                 progbar.setprogress(downloadedsize);                    float per = ((float)downloadedsize/totalsize) * 100;                 // update progress in percentage //                 tvpercent.settext((int)per + "%");                   totaltime = system.currenttimemillis() - previoustime;                 networkspeed = downloadedsize / totaltime;                   // update network speed //                 tvspeed.settext(networkspeed+"kbps");                 }            //close output stream when complete //             fileoutput.close();             downloadedsize = 0;             totalsize = 0;             }          catch (final malformedurlexception e)          {                    e.printstacktrace();         }          catch (final ioexception e)          {                      e.printstacktrace();         }         catch (final exception e)          {             e.printstacktrace();         }           return null;     }       protected void onprogressupdate(string... progress)      {         // setting progress percentage         //progbar.setprogress(integer.parseint(progress[0]));     }       @override     protected void onpostexecute(string file_url)     {      }     } 

you can update ui main thread. move tvpercent.settext((int)per + "%"); onprogressupdate , call publishprogress(.) in doinbackground whenever want update text.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -