所以我将我当前的位置与我存储在数据库中的位置进行比较。一切都很顺利但是当我使用模拟器控制发送位置时,我无法使我的单元格保持静音.Logcat说光标是OutOfBounds
这是我的代码
Cursor cur=null;
cur=y.rawQuery("SELECT longitude FROM data",null);
cur.moveToFirst();
cr=y.rawQuery("SELECT latitude FROM data",null);
cr.moveToFirst();
while(!cr.isAfterLast()) {
arlist.add(cr.getDouble(cr.getColumnIndex("latitude")));
cr.moveToNext();
}
while(!cur.isAfterLast()) {
arrylist.add(cr.getDouble(cr.getColumnIndex("longitude")));
cur.moveToNext();
}
for(int i=0;i<arlist.size();i++){
Location locB = new Location("point B");
locB.setLatitude(arlist.get(i));
locB.setLongitude(arrylist.get(i));
float distance=loc.distanceTo(locB);
if(distance<5){
((AudioManager) getSystemService(AUDIO_SERVICE))
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
else{
((AudioManager) getSystemService(AUDIO_SERVICE))
.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
这是我的logcat
11-11 09:45:16.528: E/AndroidRuntime(692): FATAL EXCEPTION: main
11-11 09:45:16.528: E/AndroidRuntime(692): android.database.CursorIndexOutOfBoundsException: Index 20 requested, with a size of 20
11-11 09:45:16.528: E/AndroidRuntime(692): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:86)
11-11 09:45:16.528: E/AndroidRuntime(692): at com.example.alert.service$MyLocationListener.onLocationChanged(service.java:90)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:237)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:170)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:186)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.os.Looper.loop(Looper.java:137)
11-11 09:45:16.528: E/AndroidRuntime(692): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-11 09:45:16.528: E/AndroidRuntime(692): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 09:45:16.528: E/AndroidRuntime(692): at java.lang.reflect.Method.invoke(Method.java:511)
11-11 09:45:16.528: E/AndroidRuntime(692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-11 09:45:16.528: E/AndroidRuntime(692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-11 09:45:16.528: E/AndroidRuntime(692): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我认为问题出现在这个循环中,您在循环cur
但尝试从cr
检索:
while(!cur.isAfterLast()) {
arrylist.add(cr.getDouble(cr.getColumnIndex("longitude")));
cur.moveToNext();
}
应该是:
while(!cur.isAfterLast()) {
arrylist.add(cur.getDouble(cur.getColumnIndex("longitude")));
cur.moveToNext();
}