android studio从sqlite数据库向google地图添加标记

android studio从sqlite数据库向google地图添加标记,sqlite,google-maps,android-studio,google-maps-markers,Sqlite,Google Maps,Android Studio,Google Maps Markers,在我的android应用程序中,我试图将几个标记从sqlite添加到MapsActivity。我创建了一个do-while循环,用于获取sqlite表中的所有城市,并转换坐标(lat,long)中的所有城市,但出现了一些错误。你能帮我吗 ---MapsActivity.java--- 公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{ 私有谷歌地图; 数据库助手myDb; @凌驾 创建时受保护的void(Bundle savedIns

在我的android应用程序中,我试图将几个标记从sqlite添加到MapsActivity。我创建了一个do-while循环,用于获取sqlite表中的所有城市,并转换坐标(lat,long)中的所有城市,但出现了一些错误。你能帮我吗

---MapsActivity.java---

公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
数据库助手myDb;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
//在Sydney添加一个标记并移动相机
悉尼LatLng=新LatLng(-34151);
LatLng ruvo=新LatLng(-45123);
mMap.addMarker(新MarkerOptions().position(sydney.title)(“悉尼的标记”);
mMap.addMarker(新MarkerOptions().position(ruvo.title)(“悉尼的标记”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(ruvo));
显示城市();
}
搜索上的公共无效(视图){
EditText textCity=(EditText)findViewById(R.id.textCity);
字符串位置=textCity.getText().toString();
List addressList=null;
如果(位置!=null){
Geocoder Geocoder=新的Geocoder(本);
试一试{
addressList=geocoder.getFromLocationName(位置,1);
}捕获(IOE异常){
e、 printStackTrace();
}
android.location.Address=addressList.get(0);
LatLng LatLng=新LatLng(address.getLatitude(),address.getLongitude());
mMap.addMarker(新的MarkerOptions().position(latLng).title(“指向的标记”);
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
公共城市(){
List addressList=null;
游标数据库=myDb.display();
if(database.getCount()==0){
Toast.makeText(这个“找不到”,Toast.LENGTH_LONG.show();
返回;
}
//所有城市的字符串数组
database.moveToFirst();
String[]cities=新字符串[database.getCount()];
int n=0;
做{
cities[n]=database.getString(3.toString();
n=n+1;
}while(database.moveToNext());
Geocoder Geocoder=新的Geocoder(本);
试一试{
addressList=geocoder.getFromLocationName(城市[n],1);
}捕获(IOE异常){
e、 printStackTrace();
}
android.location.Address=addressList.get(0);
LatLng LatLng=新LatLng(address.getLatitude(),address.getLongitude());
mMap.addMarker(新的MarkerOptions().position(latLng).title(“指向的标记”);
}
}
---DaTabaseHelper.java---

public类DatabaseHelper扩展了SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
私有静态最终字符串数据库\u NAME=“Student.db”;
公共静态最终字符串表\u NAME=“学生表”;
//非常重要:CursorAdapter完全需要“_id”列,如本文所述
公共静态最终字符串COL_1=“\u id”;
公共静态最终字符串COL_2=“NAME”;
公共静态最终字符串COL_3=“姓氏”;
公共静态最终字符串COL_4=“CITY”;
public ArrayList results=new ArrayList();
公共DatabaseHelper(上下文上下文、字符串名称、SQLiteDatabase.CursorFactory、int版本){
超级(上下文、数据库名称、工厂、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
db.execSQL(“创建表”+表名+”(“+COL\u 1+”整数主键自动递增,”
+第2列+文字、+3列+文字、+4列+文字);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
db.execSQL(“如果存在删除表”+表名称);
onCreate(db);
}
已选择公共光标(长id){
SQLiteDatabase db=this.getWritableDatabase();
字符串[]列=新字符串[]{DatabaseHelper.COL_1,DatabaseHelper.COL_2,DatabaseHelper.COL_3,DatabaseHelper.COL_4};
游标c=db.query(DatabaseHelper.TABLE_名称、列、DatabaseHelper.COL_1+“=”+id,null,null,null,null);
如果(c!=null){
c、 moveToFirst();
}
返回c;
}
公共布尔插入数据(字符串名称、字符串姓氏、字符串城市){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
ContentValues.put(COL_2,name);
ContentValues.put(COL_3,姓氏);
ContentValues.put(COL_4,城市);
long result=db.insert(表名称,null,ContentValues);
如果(结果==-1)
返回false;
其他的
返回true;
}
//选择查询的方法
公共光标显示(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery(“select*from”+表名称,null);
返回res;
}
公共布尔更新数据(字符串id、字符串名称、字符串姓氏、字符串城市){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
ContentValues.put(COL_1,id);
ContentValues.put(COL_2,name);
ContentValues.put(COL_3,姓氏);
ContentValues.put(COL_4,城市);
update(表名,ContentValues,“\u id=?”,新字符串[]{id});
返回true;
}
公共整数DeleteData(字符串id){
SQLiteDatabase db=this.getWritableDatabase();
返回db.delete(表名,“\u id=?”,新字符串[]{id});
}
}
编辑:我在logcat中添加了错误

坠机开始

06-28 12:17:13.332 2915-2915/com.example.android.sqlite E/AndroidRuntime:FATAL EXCEPTION:main 进程:com.example.android.sqlite,PID:2915 java.lang.NullPointerException:尝试调用vi
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
DatabaseHelper myDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    LatLng ruvo = new LatLng(-45, 123);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.addMarker(new MarkerOptions().position(ruvo).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(ruvo));
    displayCities();
}

public void onSearch(View view) {
    EditText textCity = (EditText) findViewById(R.id.textCity);
    String location = textCity.getText().toString();
    List<android.location.Address> addressList = null;

    if (location != null) {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        android.location.Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker pointed"));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }

}

public void displayCities() {
    List<android.location.Address> addressList = null;
    Cursor database = myDb.display();

    if (database.getCount()== 0) {
        Toast.makeText(this, "Nothing to found", Toast.LENGTH_LONG).show();
        return;
    }
    //string array of all cities
    database.moveToFirst();
    String[] cities = new String[database.getCount()];
    int n = 0;
    do {
        cities[n] = database.getString(3).toString();
        n=n+1;
    } while (database.moveToNext());

        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(cities[n], 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        android.location.Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker pointed"));
    }

}
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
//VERY IMPORTANT: CursorAdapter needs exactly "_id" column, as written here
public static final String COL_1 = "_id";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "CITY";

public ArrayList<String> results = new ArrayList<>();

public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT , "
            + COL_2 + " TEXT , " + COL_3 + " TEXT , " + COL_4 + " TEXT );");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public Cursor selected(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    String[] columns = new String[]{DatabaseHelper.COL_1, DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4};
    Cursor c = db.query(DatabaseHelper.TABLE_NAME, columns, DatabaseHelper.COL_1 + "=" + id, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

public boolean insertData(String name, String surname, String city) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues ContentValues = new ContentValues();
    ContentValues.put(COL_2, name);
    ContentValues.put(COL_3, surname);
    ContentValues.put(COL_4, city);
    long result = db.insert(TABLE_NAME, null, ContentValues);
    if (result == -1)
        return false;
    else
        return true;
}

//method for select query
public Cursor display() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}


public boolean UpdateData(String id, String name, String surname, String city) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues ContentValues = new ContentValues();
    ContentValues.put(COL_1, id);
    ContentValues.put(COL_2, name);
    ContentValues.put(COL_3, surname);
    ContentValues.put(COL_4, city);
    db.update(TABLE_NAME, ContentValues, "_id = ?", new String[] {id});
    return true;
}

public Integer DeleteData (String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "_id = ?", new String[] {id});
}


}