我的 android 应用程序连接使用 Web 服务执行外部数据库。在一种情况下,我有大量数据,并且出现内存不足异常:
09-09 20:05:59.441: ERROR/dalvikvm(4688): Out of memory: Heap Size=15751KB, Allocated=14962KB, Bitmap Size=687KB
09-09 20:05:59.451: WARN/dalvikvm(4688): threadid=23: thread exiting with uncaught exception (group=0x4001e390)
09-09 20:05:59.462: ERROR/AndroidRuntime(4688): Uncaught handler: thread AsyncTask #5 exiting due to uncaught exception
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): java.lang.RuntimeException: An error occured while executing doInBackground()
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.lang.Thread.run(Thread.java:1102)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): Caused by: java.lang.OutOfMemoryError
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:142)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.lang.StringBuilder.append(StringBuilder.java:124)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONTokener.nextString(JSONTokener.java:252)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONTokener.nextValue(JSONTokener.java:316)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONObject.<init>(JSONObject.java:204)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONTokener.nextValue(JSONTokener.java:319)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONObject.<init>(JSONObject.java:204)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONTokener.nextValue(JSONTokener.java:319)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONArray.<init>(JSONArray.java:116)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at org.json.JSONArray.<init>(JSONArray.java:150)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at se.softwerk.timelog.activity.ViewActivity.setTasks(ViewActivity.java:219)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at se.softwerk.timelog.activity.ViewActivity.fetchTasks(ViewActivity.java:133)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at se.softwerk.timelog.activity.ViewActivity.access$9(ViewActivity.java:110)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at se.softwerk.timelog.activity.ViewActivity$LoadingDataFromServer.doInBackground(ViewActivity.java:420)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at se.softwerk.timelog.activity.ViewActivity$LoadingDataFromServer.doInBackground(ViewActivity.java:1)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-09 20:05:59.731: ERROR/AndroidRuntime(4688): ... 4 more
这是我的课:
private class LoadingDataFromServer extends AsyncTask<Void, Void, Void> {
@Override
public void onPreExecute() {
progress = ProgressDialog.show(ViewActivity.this, null,"Loading ...", true);
}
@Override
protected void onPostExecute(Void result) {
try {
ListView lv = (ListView) main_activity.findViewById(R.id.listTask);
lv.setAdapter(new TaskAdapter(main_activity));
progress.dismiss();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Override
protected Void doInBackground(Void... params) {
try {
fetchTasks();
} catch (Exception e) { //DOES NOT WORK
progress.dismiss();
Toast.makeText(ViewActivity.this, "Enormous number of data, select smaller criteria", Toast.LENGTH_SHORT).show();
finish();
}
return null;
}
}
我怎样才能捕捉到这个异常doInBackground
?我目前的尝试不起作用。