Reputation: 303
I am new to Android, I have started only 7 days ago. I'm getting this type of error and also refer most of the topic in the same forum, asked for similar question, but not getting how to resolve.
Here is my code:
class CreateNewCustomer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity4.this);
pDialog.setMessage("Creating Customer..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String...args) {
String fname = inputFName.getText().toString();
String lname = inputLName.getText().toString();
String phone = inputPhone.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fname", fname));
params.add(new BasicNameValuePair("lname", lname));
params.add(new BasicNameValuePair("phone", phone));
JSONObject json = jsonParser.makeHttpRequest(url_create_customer,
"POST", params);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Intent i = new Intent(getApplicationContext(),MainActivity5.class );
startActivity(i);
finish();
} else {
// This is the Else part
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
Upvotes: 4
Views: 5122
Reputation: 20553
In this part:
if (success == 1) {
Intent i = new Intent(getApplicationContext(),
MainActivity5.class );
startActivity(i);
finish();
}
before you call finish()
you need to dismiss the progress dialog. It's not being dismissed hence the window is being leaked and it's causing an exception.
Use this code instead:
if (success == 1) {
Intent i = new Intent(getApplicationContext(), MainActivity5.class );
startActivity(i);
pDialog.dismiss();
finish();
}
Also, start your new activity from onPostExecute()
, not doInBackground()
. Use a flag to check your event for success and start a new activity in onPostExecute()
like this:
@Override
protected String doInBackground(String...args) {
//...
if (success == 1) {
successFlag=true;
}
//...
}
@Override
protected void onPostExecute(String file_url) {
if(successFlag=true) {
Intent i = new Intent(getApplicationContext(), MainActivity5.class );
startActivity(i);
pDialog.dismiss();
finish();
}
}
Upvotes: 3
Reputation: 6197
Never do a startActivity in doInBackground().
Do the startActivity after you dismiss the progress dialog in onPostExecute()
Upvotes: 0