无法将坐标从SqLite获取到地图活动

问题描述

我正在尝试从数据库获取坐标,以便可以在“地图活动”中创建标记。但是我总是会得到一个空指针异常。

这是我的数据库

String query = "CREATE TABLE "+TABLE_NAME+"("+
                COLUMN_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT,"+
                COLUMN_LAT+ " REAL,"+
                COLUMN_LONG+" REAL,"+
                COLUMN_NUM+ " INTEGER "+
                ");";

这就是我调用函数以在我的 MapActivity 获取数据库的经度和纬度值的方法

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    double[] dblat = new double[5];
//this is where I get NullPoint Exception
    dblat=dbHandler.Markerslat();
    double []dblong = new double[5];
     dblong = dbHandler.Markerslong();
    for(int j=0;j<dblat.length;j++) {

        LatLng latLng = new LatLng( dblat[j],dblong[j] );
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position( latLng );
        markerOptions.title( "Pothole" );
        markerOptions.icon(BitmapDescriptorFactory.fromresource(R.drawable.potholesmall));
        mMap.addMarker( markerOptions );
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    }
}

这是我的 DBHandler 中的2个函数

public double [] Markerslat(){
    sqliteDatabase db = getWritableDatabase();

    Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_NAME + ";",null);
    cursor.movetoFirst();
    int k = cursor.getCount();
    double []dlatitude= new double[k];
    for(int i=0;i<k;i++){

        dlatitude[i]=cursor.getDouble(1);
        Log.i(TAG,"Entered");
        cursor.movetoNext();
    }
    db.close();

    return dlatitude;

}

public double[] Markerslong(){
    sqliteDatabase db = getWritableDatabase();

    Cursor cursor = db.rawQuery("SELECT longitude FROM "+ TABLE_NAME +";",null);
    cursor.movetoFirst();
    int k = cursor.getCount();
    double []dlongitude= new double[k];
    cursor.movetoFirst();
    for(int i=0;i<k;i++){

        dlongitude[i]=cursor.getDouble(2);
        cursor.movetoNext();
    }
    db.close();

    return dlongitude;

}

请,任何建议将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)