Android AsynkTask Taking too Long Rotating Progress Dialog
-
AsyncTask takes too long to do tasks while rotating Progress Dialog, finally the application crashes displaying the message "Unfortunately "App_Name" has stopped." What should I do in onPreExecute, doInbackground, onPostExecute so as to solve this problem? What are the most likely causes this? PLEASE TELL ME IF THIS IS THE STANDARD CODE FOR AsynkTask!!! THANK YOU IN ADVANCE FOR YOUR TIME!!! The AsynkTask Code I used is below: package com.Project.mbjs; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class RegisterJobSeeker extends Activity { // Progress Dialog private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); EditText inputFirstName; EditText inputLastName; EditText inputEmail; EditText inputPassword; EditText inputExperience; EditText inputEduLevel; EditText inputPhoneNo; // URL to register Job Seeker private static String URL_RegisterJobSeeker = "http://10.0.2.2 /mbjs/RegisterJobSeeker.php"; // JSON Node names private static final String TAG_SUCCESS = "success"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registerjobseeker); // Edit Text inputFirstName = (EditText) findViewById(R.id.txtFName); inputLastName = (EditText) findViewById(R.id.txtLName); inputEmail = (EditText) findViewById(R.id.txtEmail); inputPassword = (EditText) findViewById(R.id.txtPassword); inputExperience = (EditText) findViewById(R.id.txtExperience); inputEduLevel = (EditText) findViewById(R.id.txtEduLevel); inputPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); // Create button Button btnRegister = (Button) findViewById(R.id.btnRegister); // button click event btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // creating new product in background thread new
-
AsyncTask takes too long to do tasks while rotating Progress Dialog, finally the application crashes displaying the message "Unfortunately "App_Name" has stopped." What should I do in onPreExecute, doInbackground, onPostExecute so as to solve this problem? What are the most likely causes this? PLEASE TELL ME IF THIS IS THE STANDARD CODE FOR AsynkTask!!! THANK YOU IN ADVANCE FOR YOUR TIME!!! The AsynkTask Code I used is below: package com.Project.mbjs; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class RegisterJobSeeker extends Activity { // Progress Dialog private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); EditText inputFirstName; EditText inputLastName; EditText inputEmail; EditText inputPassword; EditText inputExperience; EditText inputEduLevel; EditText inputPhoneNo; // URL to register Job Seeker private static String URL_RegisterJobSeeker = "http://10.0.2.2 /mbjs/RegisterJobSeeker.php"; // JSON Node names private static final String TAG_SUCCESS = "success"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registerjobseeker); // Edit Text inputFirstName = (EditText) findViewById(R.id.txtFName); inputLastName = (EditText) findViewById(R.id.txtLName); inputEmail = (EditText) findViewById(R.id.txtEmail); inputPassword = (EditText) findViewById(R.id.txtPassword); inputExperience = (EditText) findViewById(R.id.txtExperience); inputEduLevel = (EditText) findViewById(R.id.txtEduLevel); inputPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); // Create button Button btnRegister = (Button) findViewById(R.id.btnRegister); // button click event btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // creating new product in background thread new
I added super.onPostExecute(file_url) in the onPostExecute and has inserted the data in to DB but still AsynkTask taking too long and finally the app in emulator crashes! protected void onPostExecute(String file_url) { super.onPostExecute(file_url) // dismiss the dialog once done pDialog.dismiss(); } How can I avoid waiting too long doing BG Tasks and crashing??? What to include in my AndroidManifest.xml??? THANK YOU IN ADVANCE!
-
AsyncTask takes too long to do tasks while rotating Progress Dialog, finally the application crashes displaying the message "Unfortunately "App_Name" has stopped." What should I do in onPreExecute, doInbackground, onPostExecute so as to solve this problem? What are the most likely causes this? PLEASE TELL ME IF THIS IS THE STANDARD CODE FOR AsynkTask!!! THANK YOU IN ADVANCE FOR YOUR TIME!!! The AsynkTask Code I used is below: package com.Project.mbjs; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class RegisterJobSeeker extends Activity { // Progress Dialog private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); EditText inputFirstName; EditText inputLastName; EditText inputEmail; EditText inputPassword; EditText inputExperience; EditText inputEduLevel; EditText inputPhoneNo; // URL to register Job Seeker private static String URL_RegisterJobSeeker = "http://10.0.2.2 /mbjs/RegisterJobSeeker.php"; // JSON Node names private static final String TAG_SUCCESS = "success"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registerjobseeker); // Edit Text inputFirstName = (EditText) findViewById(R.id.txtFName); inputLastName = (EditText) findViewById(R.id.txtLName); inputEmail = (EditText) findViewById(R.id.txtEmail); inputPassword = (EditText) findViewById(R.id.txtPassword); inputExperience = (EditText) findViewById(R.id.txtExperience); inputEduLevel = (EditText) findViewById(R.id.txtEduLevel); inputPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); // Create button Button btnRegister = (Button) findViewById(R.id.btnRegister); // button click event btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // creating new product in background thread new
First you are not supposed to access UI components directly from a background thread.
String doInBackground(String... args) {
String firstName = args\[0\]; String lastName = args\[1\]; String email = args\[2\]; String password = args\[3\]; String experience = args\[4\]; String eduLevel = args\[5\]; String phoneNo = args\[6\];
// create the parser inside doInBackground
JSONParser jsonParser = new JSONParser();
.....JSONObject json = jsonParser.makeHttpRequest(URL_RegisterJobSeeker,
"POST", params);return String.valueOf(json.getInt(TAG_SUCCESS));
}
//NOTE: Only Interact with the UI components only in onPostExecute or onPreExecute
//After completing background task Dismiss the progress dialogprotected void onPostExecute(String success) { // dismiss the dialog once done pDialog.dismiss(); if (success == "1") { // successfully created product Toast.makeText(RegisterJobSeeker.this, "Job Seeker Successfully Registered.", Toast.LENGTH\_LONG).show();; // Intent i = new // Intent(getApplicationContext(),RegisterJobSeeker.class); // startActivity(i); // closing this screen // finish(); } else { // failed to create product } }
You might want to host your AsyncTask in a headless fragment so that device configuration changes don't crash your app. Hope this helps.
“Everything is simple when you take your time to analyze it.”