Android Google地图使用存储的Sqlite数据库添加标记
当我在Autocompletetextview中键入“Fair”时,我的应用程序停止Android Google地图使用存储的Sqlite数据库添加标记,android,sqlite,google-maps,Android,Sqlite,Google Maps,当我在Autocompletetextview中键入“Fair”时,我的应用程序停止 当我在Autocompletetextview中键入nothing(即空)时,它会显示我存储在sqlite中的地图中的所有位置 当我在Autocompletetextview中键入“优秀”时。它只显示了地图上的“最佳”位置 但是,当我在Autocompletetextview中键入“Fair”时。它不存储在sqlite中的任何位置。它停止了我的应用程序。我希望它显示“数据不可用”,而不是停止我的应用程序。如何更
Process: com.example.bharat.plantnow, PID: 15807
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:86)
at com.example.bharat.plantnow.MapsActivity$1.onClick(MapsActivity.java:149)
我的SQLITE数据库列包括:
//Table Columns names
private static final String KEY_TID = "id";
public static final String KEY_TreeID = "treeid";
public static final String KEY_TCONDITION = "condition";
public static final String KEY_TLATITUDE = "latitude";
public static final String KEY_TLONGITUDE = "longitude";
我的sqlite数据库:我存储了“优秀”和“良好”的值。不存储“公平”的任何值
我的地图活动:
btMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String search = inputSearch.getText().toString();
if(search.equals("")) {
double latitude = 0;
double longitude = 0;
String mark;
// SQLite database handler
db = new TreeData(getApplicationContext());
Cursor locationCursor = db.getLocations();
locationCursor.moveToFirst();
do {
latitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
longitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
mark = locationCursor.getString(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title(mark));
} while (locationCursor.moveToNext());
}
if(search.length()>0){
//float zoom = 0;
mMap.clear();
// SQLite database handler
db = new TreeData(getApplicationContext());
Cursor c = db.getSelectedLocations(search);
c.moveToFirst();
do {
double latitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
double longitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
String marker = c.getString(c
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude1, longitude1);
mMap.addMarker(new MarkerOptions().position(location).title(marker));
// Moving CameraPosition to last clicked position
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
// Setting the zoom level in the map on last position is clicked
mMap.animateCamera(CameraUpdateFactory.newLatLng(location));
} while (c.moveToNext());
Toast.makeText(getApplicationContext(), "All is Well", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_LONG).show();
}
}
});}
您有一个emtpy光标。不要检查它是否为null,而是尝试检查(mCursor.getCount()>0){//do stuff}谢谢Sasikumar。现在它的工作很好。。
btMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String search = inputSearch.getText().toString();
if(search.equals("")) {
double latitude = 0;
double longitude = 0;
String mark;
// SQLite database handler
db = new TreeData(getApplicationContext());
Cursor locationCursor = db.getLocations();
locationCursor.moveToFirst();
do {
latitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
longitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
mark = locationCursor.getString(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title(mark));
} while (locationCursor.moveToNext());
}
if(search.length()>0){
//float zoom = 0;
mMap.clear();
// SQLite database handler
db = new TreeData(getApplicationContext());
Cursor c = db.getSelectedLocations(search);
c.moveToFirst();
do {
double latitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
double longitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
String marker = c.getString(c
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude1, longitude1);
mMap.addMarker(new MarkerOptions().position(location).title(marker));
// Moving CameraPosition to last clicked position
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
// Setting the zoom level in the map on last position is clicked
mMap.animateCamera(CameraUpdateFactory.newLatLng(location));
} while (c.moveToNext());
Toast.makeText(getApplicationContext(), "All is Well", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_LONG).show();
}
}
});}