Android AsyncTask user experience and error handling-UI components can only be changed in the main thread

Source: Internet
Author: User

Android AsyncTask user experience and error handling-UI components can only be changed in the main thread

You will always use the AsyncTask class frequently, especially in network processing. Check the corrected code first: this is the normal code:

 

Class sendKeyTask extends AsyncTask
 
  
{@ Overrideprotected void onPostExecute (Integer resultCode) {// TODO Auto-generated method stubsuper. onPostExecute (resultCode); switch (resultCode) {case 6000: policyhelper. poppolicyinfo (InnerQuestionActivity. this, "user information exception", ""); break; case 6001: policyhelper. poppolicyinfo (InnerQuestionActivity. this, "Other exceptions", ""); break; case 6002: break; default: break;} // hide the input method InputMethodManager imm = (InputMethodManager) getApplicationContext (). getSystemService (Context. INPUT_METHOD_SERVICE); // display or hide the input method imm. toggleSoftInput (0, InputMethodManager. HIDE_NOT_ALWAYS); innerQuestionEdit. setText (""); // refresh new getquestiondetailtack(cmd.exe cute (1) ;}@ Overrideprotected Integer doInBackground (String... data) {// TODO Auto-generated method stubint resultCode = 4001; HttpClient client = new DefaultHttpClient (); HttpPost post = new HttpPost ("http://diandianapp.sinaapp.com/add_key.php "); stringBuilder builder = new StringBuilder (); List
  
   
ParamsList = new ArrayList
   
    
(); ParamsList. add (new BasicNameValuePair ("access_token", data [0]); paramsList. add (new BasicNameValuePair ("user_name", data [1]); paramsList. add (new BasicNameValuePair ("key_detail", data [2]); paramsList. add (new BasicNameValuePair ("question_id", data [3]); for (int I = 0; I
    
     
Some people may say that I asked doInBackground to return a parameter and then process it multiple times in onPostExecute? However, when you combine the two parts, you will find that an error is reported! Generally, the error message is that the UI content can only be changed in the main thread. Why!
     

 

Policyhelper. poppolicyinfo (InnerQuestionActivity. this, "Other exceptions", ""); is a pop-up method encapsulation of the dialog box. this is an operation on the UI interface and the problem should be solved here!

Let's look at google's instructions:

 

Protected abstract Result doInBackground (Params... params) Added in API level 3

Override this method to perform a computation on a background thread. The specified parameters are the parameters passedexecute(Params...)By the caller of this task. This method can callpublishProgress(Progress...)To publish updates on the UI thread.

Parameters
   

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.