2

私の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ですか? 私の現在の試みはうまくいきません。

4

2 に答える 2

6

OutOfmemoryErrorは Exception を拡張しません。これがキャッチしない理由です。

このコードは機能するはずです。

try
{
    fetchTasks();       
}
catch(OutOfMemoryError e)
{
    // do something     
}
于 2012-09-09T18:19:28.740 に答える
3
try {
    ...
} catch (Exception e) {  // "catch all subclasses of java.lang.Exception"
    ...
}

ただし、OutOfMemoryErrorから継承し、継承java.lang.Errorせずjava.lang.Exception、両方ともから継承しjava.lang.Throwableます。throwjavaのサブクラスのインスタンスのみを使用できるため、これでエラーまたは例外をjava.lang.Throwableキャッチできます。

try {
    ...
} catch (Throwable t) {  // "catch all subclasses of java.lang.Throwable"
    ...
}
于 2012-09-09T18:26:19.317 に答える