我正在尝试构建一个应用程序,我试图从我的数据库中获取数字,然后将它们添加到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 helper类方法的代码
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;
}
在实例化这个arraylist时我得到了这个异常
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)
答案 0 :(得分:2)
填充列表的内部循环:
while(cursor.isAfterLast() == false) {
numbers.add(cursor.getString(2));
}
cursor.isAfterLast()
可能会返回错误吗?我没有看到任何方法调用来推进光标。我怀疑,这个循环永远不会结束,用相同的值填充数组列表,直到它耗尽可用内存。提示:cursor.moveToNext()
可能有用。