Fatal Exception : Asynctask #1 Doinbackground
Im new to android programming and i cant pinpoint which triggers the error. i am having this errors: 10-29 02:34:17.989: E/AndroidRuntime(853): FATAL EXCEPTION: AsyncTask #1 10-29
Solution 1:
Do not perform the UI related operation in doInBackgroungd()
method. Just return the result in onPostExecute()
and then write the code of start activity.
/**
* After Completing background task Dismiss the progress Dialog
*/
protected void onPostExecute(String result) {
// dismiss the dialog once done
pDialog.dismiss();
if(result!=null && result.equals("1")){
// success to create trip
Intent i = new Intent(getApplicationContext(), CreateTripActivity.class);
startActivity(i);
finish();
}else{
// failed to create trip
}
}
Solution 2:
// try this way
class CreateTrip extends AsyncTask {
ProgressDialog pDialog;
/** * Before Sttarting Background thread Show Progress Dialog */
@Override protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CreateTripActivity.this);
pDialog.setMessage("Creating Trip..."); pDialog.setIndeterminate(false);
pDialog.setCancelable(true); pDialog.show();
}
/**
* Creating Trip
*/
protected String doInBackground(String... args) {
String plate = inputPlate.getText().toString();
String customer = inputCustomer.getText().toString();
String area = inputArea.getText().toString();
String driver = inputDriver.getText().toString();
//Building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("plate", plate));
params.add(new BasicNameValuePair("customer", customer));
params.add(new BasicNameValuePair("area", area));
params.add(new BasicNameValuePair("driver", driver));
//getting JSON Object
// Note that create trip url accepts post method
JSONObject json = jsonParser.makeHttpRequest(url_create_trip, "POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try{
int success = json.getInt(TAG_SUCCESS);
if (success == 1){
// success to create trip
return "1";
}else {
return "0";
// failed to create trip
}
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
/**
* After Completing background task Dismiss the progress Dialog
*/
protected void onPostExecute(String result) {
// dismiss the dialog once done
pDialog.dismiss();
if(result!=null && result.equals("1")){
// success to create trip
Intent i = new Intent(getApplicationContext(), CreateTripActivity.class);
startActivity(i);
finish();
}else{
// failed to create trip
}
}
}
Post a Comment for "Fatal Exception : Asynctask #1 Doinbackground"