0

私は次のコードを持っています:(基本的にはさまざまなJSONObjects(beneficiario)を作成し、それらをすべて別のJSONObject(proposta)内に配置します。ここでは示しませんが、前にカーソル(ppt、c)を作成しました

            if (ppt.getString(45).equals("0")) {
                int i = 0;

                c.moveToFirst();
                JSONObject ben = new JSONObject();
                try {
                    while (c.isAfterLast() == false) 
                    {
                        i++;
                        ben.put("b_nome" + i, c.getString(1));
                        ben.put("b_telefone" + i, c.getString(2));
                        ben.put("b_nif" + i, c.getString(3));
                        ben.put("b_bi" + i, c.getString(4));
                        ben.put("b_codigopostal" + i, c.getString(5));
                        ben.put("b_localidade" + i, c.getString(6));
                        ben.put("b_morada" + i, c.getString(7));                        
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                proposta.put("beneficiario" + i, ben);
            }

そして、メモリ不足エラーが発生します。これは、メインスレッドで実行しているためだと思います。スレッドまたは非同期タスクを使用してそれを行うためのヘルプ/コードを教えてもらえますか?

4

2 に答える 2

3

ループで使用c.moveToNext();します。WhileあなたCursorは無限を実行しています。

于 2012-07-17T12:15:00.680 に答える
0

次のような非同期タスクを使用します。

// You must provide types for the three generic parameters before the code will compile.          
// For more details, see http://developer.android.com/reference/android/os/AsyncTask.html 
 private class MoveOutOfUIthread extends AsyncTask<
                Params, // one or more values of this type are passed to doInBackground()
                Progress, // the type of the progress units published during background crunching.
                Result // the type of the result returned by doInBackground()
                > 
 {

         protected Integer doInBackground(Params... p1, p2, p3) {
             // your background task here
             Result result = new Result();
             return result;
         }

         protected void onPostExecute(Result r) {
             // this gets the object returned by doInBackground, and executes on the UI thread
         }
 }   

*次のように非同期タスクを実行します: *

new MoveOutOfUIthread().execute(p1, p2, p3);

パラメータを渡す必要はありません。これを簡単に行うことができます:

new MoveOutOfUIthread().execute();

注:で UI 要素を変更することはできませんdoInBackground。コードを挿入するonPreExecuteonPostExecute、UI を変更する必要があります。

アップデート :

これは、バックグラウンド スレッドでコードを実行することです。そして、あなたの OutOfMemory エラーに従って、答えc.moveToNext();として while ループで使用する必要があると思います。M Mohsin Naeem

于 2012-07-17T12:20:18.320 に答える