gpt4 book ai didi

java - 如何在 AsyncTask 中捕获内存不足异常

转载 作者:太空狗 更新时间:2023-10-29 15:55:22 26 4
gpt4 key购买 nike

我的 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 中捕获此异常?我目前的尝试不起作用。

最佳答案

OutOfmemoryError不扩展异常,这就是您不捕获它的原因。

这段代码应该可以工作。

try
{
fetchTasks();
}
catch(OutOfMemoryError e)
{
// do something
}

关于java - 如何在 AsyncTask 中捕获内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12341614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com