android studio从sqlite数据库添加标记到谷歌地图

时间:2016-06-28 10:34:18

标签: sqlite google-maps android-studio google-maps-markers

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

--- --- MapsActivity.java

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"));
    }

}

--- --- DaTabaseHelper.java

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});
}


}

编辑:我在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:尝试调用虚方法&#39; android.database.Cursor com.example.android.sqlite.DatabaseHelper.display()&#39;在空对象引用上

     

at com.example.android.sqlite.MapsActivity.displayCities(MapsActivity.java:76)
  在com.example.android.sqlite.MapsActivity.onMapReady(MapsActivity.java:51)
  在com.google.android.gms.maps.SupportMapFragment $ zza $ 1.zza(未知来源)
  在com.google.android.gms.maps.internal.zzo $ zza.onTransact(未知来源)
  在android.os.Binder.transact(Binder.java:387)
  在com.google.android.gms.maps.internal.be.a(SourceFile:82)
  在com.google.maps.api.android.lib6.e.fb.run(未知来源)
  在android.os.Handler.handleCallback(Handler.java:739)
  在android.os.Handler.dispatchMessage(Handler.java:95)
  在android.os.Looper.loop(Looper.java:148)
  在android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  06-28 12:17:15.410 2915-2915 / com.example.android.sqlite I / Process:发送信号。 PID:2915 SIG:9

0 个答案:

没有答案