Android AlertDialog Activity & Async Error - android

I have a fully functional asynctask in my android app, but when I'm not connected it causes my app to crash in the Error message within my Activity (in AlertDialog.Builder) stemming from the Async not connecting. I pass Context to my async, so that may have something to do with it, but not sure.
Below is the code from Async class and Activity. LogCat is telling me error is occurring in the AlertDialog alert builder.create(). How can I solve?
From Async class:
InputsRecapUploadTask extends AsyncTask<String, Void, Integer> {
public InputsRecapUploadTask(InputsRecap activity,
ProgressDialog progressDialog, Context ctx) {
this.activity = activity;
this.myCtx = ctx;
this.progressDialog = progressDialog;
}
#Override
protected void onPreExecute() {
progressDialog.show();
}
}
#Override
protected Integer doInBackground(String... arg0) {
//// http code
responseCode = 1;
}
}
} catch (Exception e) {
e.printStackTrace();
progressDialog.dismiss();
activity.showLoginError("");
}
return responseCode;
}
#Override
protected void onPostExecute(Integer headerCode) {
progressDialog.dismiss();
if (headerCode == 1)
activity.login(id);
else
activity.showLoginError("");
}
Activity Class:
public void showLoginError(String result) {
AlertDialog.Builder builder = new AlertDialog.Builder(InputsRecap.this);
builder.setPositiveButton("okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setMessage("unable to upload database");
AlertDialog alert = builder.create();
alert.setCancelable(true);
alert.show();
}

If an exception is thrown in your doInBackground method these two lines:
progressDialog.dismiss();
activity.showLoginError("");
Will cause Exception - you can not modify the UI from within the doInBackground method. Instead set a flag and show the error dialog in the onPostExecute which is executed in the main thread.
Check the link below especially the topic under heading The 4 steps.
http://developer.android.com/reference/android/os/AsyncTask.html

Related

How to show AlertDialog in AsyncTask without leaking Context?

I have the below code working fine to find and read a record in a Room database via an id. Android Studio required adding a try/catch block which I've included below.
Two questions:
Can I leave the if{} section blank in onPostExecute() if there is no Exception?
How do I show an AlertDialog here without leaking contect and without using a hack via WeakReference?
// AsyncTask for reading an existing CardView from the database.
private static class ReadAsyncTask extends AsyncTask<Integer, Void, Quickcard> {
private QuickcardDao asyncTaskDao;
Exception e;
ReadAsyncTask(QuickcardDao dao) {
asyncTaskDao = dao;
}
#Override
public Quickcard doInBackground(final Integer... params) {
Quickcard result;
try {
result = asyncTaskDao.readCardForUpdate(params[0]);
} catch (Exception e) {
this.e = e;
result = null;
}
return result;
}
#Override
protected void onPostExecute(Quickcard quickcard) {
if (e == null) {
// *** Okay to leave this blank? If not, what should go here?
}
else {
// *** How do I show an AlertDialog here with leaking context?
}
}
}
You cannot use a view object while using a thread that operates in background.. U must implement the dialog in the UI thread. When you are implementing the the asynchronous class, in that method you should show the alert dialog. Hope this helps..
This is what I will do to prevent leaks from happening.
private static class showDialog extends AsyncTask<String, String, String> {
private WeakReference<MainActivity> mainActivityWeakReference;
showDialog(MainActivity mainActivity){
this.mainActivityWeakReference = new WeakReference<>(mainActivity);
}
#Override
protected String doInBackground(String... params) {
//do your long long time consuming tasks here.
return "Done";
}
#Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
//Just building an alert dialog to show.
if (mainActivityWeakReference.get() != null){
final MainActivity activity = mainActivityWeakReference.get();
new AlertDialog.Builder(activity).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(activity, "Yes was clicked", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(String... text) {
}
}

Why am I getting a NullPointerException with AlertDialog in an asyncTask?

I have a splash screen that runs an asyncTask that downloads data from an API. On that task's OnPostExecute I run the next asyncTask to send stored emails. Once that is complete I need an AlertDialog to popup with an ok button so the user knows the downloads are complete. I used this SO question to get as far as I have:
Android AlertDialog inside AsyncTask
Now I'm getting a NullPointerException when I attempt to add properties to the dialog:
public class JSONParser extends AsyncTask<String, String, JSONObject> {
Context c;
public JSONParser(int api,Context c) {
this.api= api;
this.c = c;
}
...
protected void onPostExecute(JSONObject result) {
JSONObject output = new JSONEmailParser(c).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new String[] {null,null,null,null}).get();
}
}
public class JSONEmailParser extends AsyncTask<String, String, JSONObject> {
Context c;
AlertDialog.Builder builder;
public JSONEmailParser(Context c){
this.c = c;
}
protected void onPreExecute(int api){
builder = new AlertDialog.Builder(SplashScreen.this);
}
...
protected void onPostExecute(JSONObject result) {
setLastUpdate();
builder.setTitle("Sales Toolkit");
builder.setCancelable(false);
builder.setMessage("Download Complete");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
endSplash();
}
});
builder.show();
}
}
The error is coming up on builder.setTitle("Sales Toolkit");
AsyncTask#onPreExecute() doesn't take an int argument. Since your method has the wrong signature, it is likely never being called, and therefore builder is never set. This is a classic example of why you should use #Override annotations.
Do not use a .get() to execute an AsyncTask as it will not be async anymore. And onPreExecute will not be called?
It seems like the onPreExecute() method is not being called. If you don't really need to use the builder anywhere before the onPostExecute() method, I would suggest just moving
builder = new AlertDialog.Builder(SplashScreen.this);
into the onPostExecute() method.

Android ProgressDialog and AlertDialog with AsyncTask incompatibility?

I've isolated an issue I am having when showing a ProgressDialog while an AsyncTask is executing and then trying to show an AlertDialog when onPostExecute is executed.
The steps to reproduce are:
1-Run the task
2-Before the task has finished press Home button
3-When you assume the task is finished return to the app
4-AlertDialog is not shown and a low bright (like disabled) screen is shown.
I've discovered that moving the dialog.dismiss() line to the end of the onPostExecute method fixes the problem... But this is weird, isn't it?
Source code:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickButton(View view) {
new MyAsyncTask(this).execute();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog;
Context ctx;
public MyAsyncTask(Context ctx) {
this.ctx = ctx;
}
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(ctx);
dialog.setMessage("Loading");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void value) {
dialog.dismiss();
AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
alertbox.setNeutralButton("OK", null);
alertbox.setMessage("Message");
alertbox.setIcon(android.R.drawable.ic_dialog_alert);
alertbox.show();
// dialog.dismiss(); If I remove it from above and put this line here, it will work ok
}
}
}

Retry AsyncTask

For example I have following AsyncTask:
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
#Override
protected Void doInBackground(Void... params) {
try {
//some code that may throws exception
return true;
} catch (IOException ex) {
return false;
}
}
#Override
protected void onPostExecute(Boolean param){
if (!param) {
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setMessage("Error");
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//I want to retry MyAsyncTask here
}
});
builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
What is best practice to do this? I'm afraid of recursion this code.
You cannot strictly "retry" an instance of AsyncTask. You have to create a new instance and execute it, just as you did the first one. You should not encounter a recursion problem.
I retry the code within doInBackground() for x number of times:
#Override
protected Void doInBackground(Void... params)
{
final int MAX_RETRY=3;
int iLoop;
boolean bSuccess=true;
for (iLoop=0; iLoop<MAX_RETRY; iLoop++)
{
try
{
//do some code that throw an exception
//success! exit loop
iLoop=0;
break;
}
catch (IOException ex)
{
bSuccess=false;
}
}
if (iLoop==(MAX_RETRY-1))
{
bSuccess=false;
}
return bSuccess;
}
This is also one of those times when the two values of a boolean are inadequate to measure success. You could replace bSuccess with an enum for a third measure of success: retry later.
I solved the same problem using BetterASyncTask.
It provides the handy HandleError abstract method, which allows me to catch the exception on the UI thread and decide if retry and how.
Please take a look at retryableasynctask and see if it helps.
Usage:
// Params, Progress and Result could be anything, same as a regular AsyncTask
new RetryableAsyncTask<Params, Progress, Result>(myActivity) {
#Override
protected void onPreExecute() {
// write some code here
}
#Override
protected Result doInBackground(Params... params) {
// execute some expensive task here with your params
// eg: MyExpensiveTask with method called 'get'
return MyExpensiveTask.get(params);
}
#Override
protected void onPostExecute(Result result) {
// write some code here with your result
}
}.execute(myParams);
Overriding "onError" behaviour
By the default, onError method shows a dialog "Cancel" and "Retry" button options. However, you might wanna do something else when something goes wrong. To do so, override onError with your own error handling.
// Params, Progress and Result could be anything, same as a regular AsyncTask
new RetryableAsyncTask<Params, Progress, Result>(myActivity) {
// ...
#Override
protected void onError(Throwable error, final Params... params) {
// write your own error handling
}
}.execute(myParams);

Application crashes while doing AlertDialog.Builder create() method - Android

I am testing my application on an LG Eve phone. I have an application that tries to download something from the web, and when it throws an exception, it is supposed to launch an alertdialog saying that there was an error. When the phone has no wifi signal, the program crashes at builder.create() (see code below). However, when there is wifi signal, and the exception is thrown by something else (for instance, a typo in the url), the dialog launches the way it's supposed to. Any clue as to why this could be?
Code for onCreateDialog:
#Override
protected Dialog onCreateDialog(int id){
Dialog d = null;
switch (id){
case DIALOG_DATA_ERROR_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getResources().getString(R.string.error_data));
builder.setCancelable(false);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int id){
d.cancel();
}
});
d = builder.create();
break;
}
return d;
}
Code for AsyncTask that calls showDialog:
private static class DownloadJSONTask extends AsyncTask<String, Void, String>{
private ProgressDialog dialog;
private Activity parent;
private JSONParserInterface jsonParser;
public DownloadJSONTask(Activity parent, JSONParserInterface jsonParser){
this.parent = parent;
this.jsonParser = jsonParser;
}
protected void onPreExecute(){
dialog = ProgressDialog.show(parent, "Loading", "Please Wait...", true);
}
protected String doInBackground (String... urls){
try {
return HttpHelper.getResponse(urls[0]);
}catch (Exception e){
dialog.cancel();
parent.showDialog(BoomSetListActivity.DIALOG_DATA_ERROR_ID);
}
return null;
}
protected void onPostExecute(String json){
dialog.cancel();
if (jsonParser != null) jsonParser.parse(json);
}
}
Do not show dialog at doInBackground. The method doesn't run on UI thread. Try showing error dialog at onPostExecute or onProgressUpdate.

Categories

Resources