Reputation: 9994
I am connecting from my Android app to a RESTful Web Service in Java. I am using AsyncTask to fetch the data. It works perfectly for normal amount of data. but when I have a huge amount of data, an Exception happens in the Android app. I tried to catch this exception, but I couldn't:
private class LoadingDataFromServer extends AsyncTask<Void, Void, Void> {
private boolean outOfMemory = false;
@Override
public void onPreExecute() {
progress = ProgressDialog.show(main_activity, null,"Loading ...", true);
}
@Override
protected void onPostExecute(Void result) {
if(outOfMemory) {
Toast.makeText(StatisticActivity.this, "Out of memory, select smaller criteria", Toast.LENGTH_SHORT).show();
finish();
}
else
setListView();
try {
progress.dismiss();
} catch (Exception e) { }
}
@Override
protected Void doInBackground(Void... params) {
TextView txtProject = (TextView) main_activity.findViewById(R.id.project);
txtProject.setBackgroundColor(Color.DKGRAY);
txtProject.setText(project);
TextView label = (TextView) main_activity.findViewById(R.id.label);
label.setText(R.string.deliverable);
try {
loadStatistic();
} catch (OutOfMemoryError e) {
outOfMemory = true;
progress.dismiss();
} catch (RuntimeException e) {
outOfMemory = true;
progress.dismiss();
} catch (Exception e) {
outOfMemory = true;
progress.dismiss();
}
return null;
}
}
This is the error I got:
10-14 22:24:10.952: ERROR/AndroidRuntime(4179): Uncaught handler: thread AsyncTask #3 exiting due to uncaught exception
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): java.lang.RuntimeException: An error occured while executing doInBackground()
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.lang.Thread.run(Thread.java:1102)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at android.os.Handler.<init>(Handler.java:121)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at android.widget.Toast.<init>(Toast.java:68)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at android.widget.Toast.makeText(Toast.java:231)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at se.softwerk.timelog.statistic.StatisticActivity$LoadingDataFromServer.doInBackground(StatisticActivity.java:301)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at se.softwerk.timelog.statistic.StatisticActivity$LoadingDataFromServer.doInBackground(StatisticActivity.java:1)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-14 22:24:10.962: ERROR/AndroidRuntime(4179): ... 4 more
The exception is happening in doInBackground
, but my exception handeling did not work. Can you help me?
Upvotes: 0
Views: 492
Reputation: 117589
You should call any UI-related code from within onPreExecute()
, onProgressUpdate(Progress... values)
or onPostExecute(Result result)
(which run in main thread, the UI thread), and use doInBackground(Params... params)
only to perform CPU-intensive operations.
Calling publishProgress(Progress... values)
within doInBackground(Params... params)
will send data to onProgressUpdate(Progress... values)
, where you can use it to update the UI.
Upvotes: 1