MainActivity leaked out the window

I am new to Android, I only started working 7 days ago. I get this type of error, and also refer to most topics in one forum, I asked a similar question, but I don’t get how to solve it.

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(); } } 
+4
source share
2 answers

In this part:

 if (success == 1) { Intent i = new Intent(getApplicationContext(), MainActivity5.class ); startActivity(i); finish(); } 

before you call finish() , you need to cancel the progress dialog. This does not quit, so the window leaks and throws an exception.

Use this code instead:

 if (success == 1) { Intent i = new Intent(getApplicationContext(), MainActivity5.class ); startActivity(i); pDialog.dismiss(); finish(); } 

Also run a new action with onPostExecute() , not doInBackground() . Use the flag to check your event for success and start a new action in onPostExecute() as follows:

 @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(); } } 
+3
source

Never do startActivity in doInBackground ().

Do startActivity after you release the run dialog in onPostExecute ()

0
source

Source: https://habr.com/ru/post/1438576/


All Articles