2013-05-06 126 views
0

他們mentionned的錯誤發生了,而excecuting doInBackground()致命異常:#的AsyncTask 5

public class NewClientActivity extends Activity { 

// Progress Dialog 
private ProgressDialog pDialog; 

JSONParser jsonParser = new JSONParser(); 
EditText name; 
EditText login; 
EditText password; 
EditText rePassword; 
EditText email; 
EditText adresse; 
EditText tel; 

// url to create new product 
private static String url_add_client = "http://192.168.1.3/android_connect/add_client.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity1); 

    // Edit Text 
    name = (EditText) findViewById(R.id.textViewNom); 
    login = (EditText) findViewById(R.id.textViewLogin); 
    password = (EditText) findViewById(R.id.textViewPassword); 
    rePassword = (EditText) findViewById(R.id.textViewPassword1); 
    email = (EditText) findViewById(R.id.textViewEmail); 
    adresse = (EditText) findViewById(R.id.textViewAdresse); 
    tel = (EditText) findViewById(R.id.textViewTel); 

    // Create button 
    Button btnAddClient = (Button) findViewById(R.id.connect); 

    // button click event 
    btnAddClient.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      // creating new product in background thread 
      new CreateNewProduct().execute(); 
     } 
    }); 
} 

/** 
* Background Async Task to Create new product 
* */ 
class CreateNewProduct extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(NewClientActivity.this); 
     pDialog.setMessage("Adding Customer to DataBase.."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    /** 
    * Creating product 
    * */ 
    protected String doInBackground(String... args) { 
     String Name = name.getText().toString(); 
     String Login = login.getText().toString(); 
     String Password = password.getText().toString(); 
    // String RePassword = rePassword.getText().toString(); 
     String Email = email.getText().toString(); 
     String Adresse = adresse.getText().toString(); 
     String Tel = tel.getText().toString(); 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("Name", Name)); 
     params.add(new BasicNameValuePair("Login", Login)); 
     params.add(new BasicNameValuePair("Password", Password)); 
     params.add(new BasicNameValuePair("Email", Email)); 
     params.add(new BasicNameValuePair("Adresse", Adresse)); 
     params.add(new BasicNameValuePair("Tel", Tel)); 


     // getting JSON Object 
     // Note that create product url accepts POST method 
     JSONObject json = jsonParser.makeHttpRequest(url_add_client, 
       "POST", params); 

     // check log cat fro response 
     Log.d("Create Response", json.toString()); 

     // check for success tag 
     try { 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // successfully created product 
      // Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); 
      // startActivity(i); 

       // closing this screen 
      // finish(); 
      } else { 
      Toast.makeText(getApplicationContext(), "L'inscription n'a pas été éfectuée correctement", Toast.LENGTH_SHORT).show();    } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once done 
     pDialog.dismiss(); 
    } 

} 
} 

請,如果你能檢查出來,並告訴我哪裏出錯? 預先感謝您!

+2

你不能把土司的AsyncTask – 2013-05-06 11:48:22

回答

1

只是覆蓋到您的AsyncTask方法onProgressUpdate

class CreateNewProduct extends AsyncTask<String, String, String> { 
    ... 
    ... 
    @Override 
    protected void onProgressUpdate(String... values) { 
     Toast.makeText(getApplicationContext(), values[0], Toast.LENGTH_SHORT).show(); 
    } 

    /** 
    * Creating product 
    * */ 
    protected String doInBackground(String... args) { 
     ... 
     // check for success tag 
     try { 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // successfully created product 
       // Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); 
       // startActivity(i); 

       // closing this screen 
       // finish(); 
      } else { 
       publishProgress("L'inscription n'a pas été éfectuée correctement"); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     ... 
    } 
} 
+1

+1替代方法 – Pragnani 2013-05-06 12:26:25

0
Toast.makeText(getApplicationContext(), "L'inscription n'a pas été éfectuée correctement", Toast.LENGTH_SHORT).show(); 

您正試圖從後臺線程更新UI,即顯示烤麪包。在onPostExecute

顯示烤麪包(),或使用runonuithread

runOnUiThread(new Runnable() //run on ui thread 
     { 
     public void run() 
     { 

     Toast.makeText(NewClientActivity.this,"mymessage", 1000).show(); 
     } 
     }); 

代替getApplicationContext的()

When to call activity context OR application context?

有關詳細信息檢查下面的鏈路使用活動上下文。

http://developer.android.com/reference/android/os/AsyncTask.html

+0

我不覺得,因爲他使用的AsyncTask他將得到'NetworkOnUIThreadException'。並且在'doInBackGround'中獲取視圖值永遠不會導致異常,這是合法的。而主要的問題是'吐司' – Pragnani 2013-05-06 11:49:00

+0

@Pragnani你是對的。更新我的答案 – Raghunandan 2013-05-06 11:50:22

+0

@Pragnani可以從doitebackground()中的editext獲取文本嗎? – Raghunandan 2013-05-06 12:03:10

1

我相信錯誤是因爲你正試圖以更新的AsyncTask的doInBackground方法的UI。

Toast.makeText(getApplicationContext(), "L'inscription n'a pas été éfectuée correctement", Toast.LENGTH_SHORT).show(); 

將上面的代碼移動到postExecute。

此外,在doInBackground下面的代碼將不可能導致發生錯誤

String Name = name.getText().toString(); 
    String Login = login.getText().toString(); 
    String Password = password.getText().toString(); 

    String Email = email.getText().toString(); 
    String Adresse = adresse.getText().toString(); 
    String Tel = tel.getText().toString();