我的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
中捕获此异常?
我目前的尝试不起作用。
答案 0 :(得分:6)
OutOfmemoryError没有扩展Exception,这就是你没有抓住它的原因。
此代码应该有效。
try
{
fetchTasks();
}
catch(OutOfMemoryError e)
{
// do something
}
答案 1 :(得分:2)
try {
...
} catch (Exception e) { // "catch all subclasses of java.lang.Exception"
...
}
但是,OutOfMemoryError
继承自java.lang.Error
而非java.lang.Exception
,并且都继承自java.lang.Throwable
。您在java中只能throw
java.lang.Throwable
个子类的实例,因此您可以使用以下方法捕获任何错误或异常:
try {
...
} catch (Throwable t) { // "catch all subclasses of java.lang.Throwable"
...
}