我正在从我的 Android 应用程序连接到 Java 中的 RESTful Web 服务。我正在使用 AsyncTask 来获取数据。它适用于正常数量的数据。但是当我有大量数据时,Android 应用程序中会发生异常。我试图捕捉这个异常,但我不能:
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;
}
}
这是我得到的错误:
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
异常发生在 中doInBackground
,但我的异常处理不起作用。你能帮助我吗?