0

在我的程序中,我有一个 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;
}
4

3 回答 3

0

你写了:

    if (cursor.moveToFirst()){
           lat = cursor.getString(cursor.getColumnIndex("lat"));
           lng = cursor.getString(cursor.getColumnIndex("lng"));
           cordarr.add(lat);
           cordarr.add(lng);
     }

但是您必须将光标安全地设置到第一个位置,或者如果光标首先打开,您真的只想获取数据吗?如果没有,请致电

        cursor.moveToFirst();

然后获取您的数据,但在 if 子句中:

          if (cursor.moveToFirst()){
    lat = cursor.getString(cursor.getColumnIndex("lat"));
    lng = cursor.getString(cursor.getColumnIndex("lng"));
    cordarr.add(lat);
    cordarr.add(lng);

     cursor.moveToNext(); //call this, so the cursor goes to the next
}  
于 2013-01-22T13:57:56.873 回答
0

尝试迭代光标。

if(cursor!=null)
 {
    cursor.moveToFirst();
    do {
    lat = cursor.getString(cursor.getColumnIndex("lat"));
    lng = cursor.getString(cursor.getColumnIndex("lng"));
    cordarr.add(lat);
    cordarr.add(lng);
   }while(cursor.moveToNext()); //It will move to next value.
  } 
于 2013-01-22T14:03:05.187 回答
0

你应该在这里有空检查,

    public ArrayList<String> getCoords(Context context, String lat, String lng) {
        DatabaseHandler db = new DatabaseHandler(context);
        Cursor cursor = db.getLatLng();
        if (cursor !null && cursor.moveToFirst()){
              ArrayList<String> cordarr = new ArrayList<String>();
            lat = cursor.getString(cursor.getColumnIndex("lat"));
            lng = cursor.getString(cursor.getColumnIndex("lng"));
            cordarr.add(lat);
            cordarr.add(lng);
            return cordarr;
        }
    cursor.close();
    db.close();
    return null;
    }

在这里,

    UserFunctions userFunc = new UserFunctions();
    ArrayList<String> cordarr = new ArrayList<String>();
    cordarr = userFunc.getCoords(getApplicationContext(), Lat1, Lng1);
    if(cordarr != null && cordarr.size() > 1) {
    Log.d("LOG","have got lat long to show.")
    Lat1 = cordarr.get(0);
    Lng1 = cordarr.get(1);
    lat = goDouble(Lat1);
    lng  = goDouble(Lng1);
    LatLng sala = new LatLng(lat, lng); 
    } else {
        Log.d("LOG","no lat long found")
    }

你能试试这个,让我看看你得到了什么日志

于 2013-01-22T14:08:38.343 回答