0

データベースから数値を取得して ArrayList に追加しようとしているアプリを構築しようとしています。これが create メソッドのサービスのコードです

public void onCreate() {
    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    geocoder = new Geocoder(sendmessage.this);
    numbers=new ArrayList<String>();
    servicerunning = true;
    Log.d("here", "till here");

    ph = new phoneblock(this);
    try {
        cursor = ph.getAllContacts();
         cursor.moveToFirst();
            if(cursor!=null){
            while(cursor.isAfterLast()==false){

                numbers.add(cursor.getString(2));


            }}
            cursor.close();
    } catch (Exception e) {
    Log.d("error",e.toString());
    }

    msg = new messageholder();

    // TODO Auto-generated method stub
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorListener = new sendmessage();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "partial wakelock");
    wl.acquire();

    mSensorListener.setOnShakeListener(new sendmessage.OnShakeListener() {

        public void onShake() {
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            v.vibrate(500);

                    sendtoall();



        }
    });
    super.onCreate();

}`

これは、カーソルを取得する mydatabse ヘルパー クラス メソッドのコードです。

public Cursor getAllContacts() {

        // Select All Query
     SQLiteDatabase mdb = this.getWritableDatabase();
     Cursor mCursor = mdb.query(TABLE_CONTACTS, new String[] {KEY_ID,
                 KEY_NAME, KEY_PH_NO}, 
                null, null, null, null, null);

              if (mCursor != null) {
               mCursor.moveToFirst();
              }
              mdb.close();
              return mCursor;
    }

この配列リストをインスタンス化すると、この例外が発生します

09-06 12:07:46.733: E/AndroidRuntime(339): FATAL EXCEPTION: main
09-06 12:07:46.733: E/AndroidRuntime(339): java.lang.OutOfMemoryError
09-06 12:07:46.733: E/AndroidRuntime(339):  at java.util.ArrayList.add(ArrayList.java:123)
09-06 12:07:46.733: E/AndroidRuntime(339):  at com.example.listview.sendmessage.onCreate(sendmessage.java:128)
09-06 12:07:46.733: E/AndroidRuntime(339):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
09-06 12:07:46.733: E/AndroidRuntime(339):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
09-06 12:07:46.733: E/AndroidRuntime(339):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
09-06 12:07:46.733: E/AndroidRuntime(339):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 12:07:46.733: E/AndroidRuntime(339):  at android.os.Looper.loop(Looper.java:123)
09-06 12:07:46.733: E/AndroidRuntime(339):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-06 12:07:46.733: E/AndroidRuntime(339):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 12:07:46.733: E/AndroidRuntime(339):  at java.lang.reflect.Method.invoke(Method.java:507)
09-06 12:07:46.733: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 12:07:46.733: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 12:07:46.733: E/AndroidRuntime(339):  at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

2

Your inner loop to fill the list:

while(cursor.isAfterLast() == false) {
   numbers.add(cursor.getString(2));
}

Can cursor.isAfterLast() ever return false? I don't see any method call to advance the cursor. I suspect, that this loop is never ending, filling the array list with the same value until it exhausts the memory available. Hint : cursor.moveToNext() may be useful.

于 2013-09-06T06:52:15.153 に答える