在我的程序中,我有一个 SQLite,它在我的应用程序中存储谷歌地图的 GPS 坐标。我尝试检索纬度和经度并将它们存储在 arrayList 中,但是当我运行程序时程序会抛出 indexOutOfBoundsException。我隐约明白,这意味着当我尝试从中检索项目时,arrayList 中没有任何内容,但如果我错了,请纠正我。
日志猫
01-22 14:39:54.750: E/AndroidRuntime(21639): FATAL EXCEPTION: main
01-22 14:39:54.750: E/AndroidRuntime(21639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidbasic12/com.example.androidbasic12.map}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.os.Looper.loop(Looper.java:137)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-22 14:39:54.750: E/AndroidRuntime(21639): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 14:39:54.750: E/AndroidRuntime(21639): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 14:39:54.750: E/AndroidRuntime(21639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-22 14:39:54.750: E/AndroidRuntime(21639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-22 14:39:54.750: E/AndroidRuntime(21639): at dalvik.system.NativeStart.main(Native Method)
01-22 14:39:54.750: E/AndroidRuntime(21639): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-22 14:39:54.750: E/AndroidRuntime(21639): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-22 14:39:54.750: E/AndroidRuntime(21639): at java.util.ArrayList.get(ArrayList.java:304)
01-22 14:39:54.750: E/AndroidRuntime(21639): at com.example.androidbasic12.map.onCreate(map.java:58)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.Activity.performCreate(Activity.java:4470)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-22 14:39:54.750: E/AndroidRuntime(21639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-22 14:39:54.750: E/AndroidRuntime(21639): ... 11 more
“DatabaseHandler.class”类中的原始查询
public Cursor getLatLng(){
SQLiteDatabase db = this.getReadableDatabase();
String query = ("SELECT * FROM " + TABLE_CARCOORD + " WHERE name = " + CAR_KEY_NAME);
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
类“UserFunction.class”
public ArrayList<String> getCoords(Context context, String lat, String lng) {
DatabaseHandler db = new DatabaseHandler(context);
Cursor cursor = db.getLatLng();
ArrayList<String> cordarr = new ArrayList<String>();
if (cursor.moveToFirst()){
lat = cursor.getString(cursor.getColumnIndex("lat"));
lng = cursor.getString(cursor.getColumnIndex("lng"));
cordarr.add(lat);
cordarr.add(lng);
}
cursor.close();
db.close();
return cordarr;
}
地图类
UserFunctions userFunc = new UserFunctions();
ArrayList<String> cordarr = new ArrayList<String>();
cordarr = userFunc.getCoords(getApplicationContext(), Lat1, Lng1);
Lat1 = cordarr.get(0);
Lng1 = cordarr.get(1);
lat = goDouble(Lat1);
lng = goDouble(Lng1);
LatLng sala = new LatLng(lat, lng);
接受字符串并转换为双精度以在 googlemaps 中使用的函数
public Double goDouble(String Latlng) {
Double goDouble = Double.parseDouble(Latlng);
return goDouble;
}