我想在地图上长按一下就保存坐标信息。我有错误的两个类:
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap)
{
mGoogleMap = googleMap;
if(mGoogleMap == null)
{return;}
else {
mGoogleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
mGoogleMap.addMarker(new MarkerOptions().position(latLng).title("Sei qui").snippet(coordinateList));
ContentValues contentValues = new ContentValues();
coordinateList = latLng.latitude + ", " + latLng.longitude;
contentValues.put("coordinate", coordinateList);
dbManager.setProvider(contentValues);
}
});
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker)
{
/*String id = marker.getId();
String position = marker.getPosition().toString();
marker.setSnippet(id + "-" + position);
marker.showInfoWindow();*/
marker.getId();
marker.getTitle();
marker.getSnippet();
marker.showInfoWindow();
return false;
}
});
mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(final Marker marker) {
View v = getLayoutInflater(bundle).from(getActivity()).inflate(R.layout.snipper_layout, null);
TextView id = (TextView) v.findViewById(R.id.id);
TextView descrizione = (TextView) v.findViewById(R.id.descrizione);
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(marker.getTitle());
descrizione.setText(marker.getSnippet());
id.setText("ID: " + marker.getId());
return v; //inflate e inserimento dati
}
});
mGoogleMap.setOnInfoWindowLongClickListener(new GoogleMap.OnInfoWindowLongClickListener()
{
@Override
public void onInfoWindowLongClick(Marker marker)
{
mostraToast("hai toccato l'ID: " + marker.getId());
openDialog(marker);
}
});
}
}
});
给我错误的行:
contentValues.put("coordinate", coordinateList);
和
public class DbManager
{ private DbHelper helper = null;
public DbManager(Context context)
{
helper=new DbHelper(context);
}
public void setProvider(ContentValues contentValues)
{
SQLiteDatabase db = helper.getWritableDatabase();
db.insert("tab", null, contentValues);
}
public List<String> getProviderss()
{
SQLiteDatabase db=helper.getReadableDatabase();
Cursor crs=db.query("tab", null, null, null, null, null, null);
List<String> res;
res = new ArrayList<String>();
while(crs.moveToNext())
{
String cord = crs.getString(crs.getColumnIndex("coordinate"));
//String altezzaPal = crs.getString(crs.getColumnIndex("altezzapalo"));
//String paloTipo = crs.getString(crs.getColumnIndex("palotipo"));
res.add(cord /*+ altezzaPal + paloTipo*/);
}
return res;
}
}
我的SQLiteDatabase类:
public class DbHelper extends SQLiteOpenHelper
{
public DbHelper(Context context)
{
super(context, "Providerss", null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{ }
@Override
public void onCreate(SQLiteDatabase db)
{
String query = "CREATE TABLE tab " +
"(" +
"_id integer primary key autoincrement, " +
"coordinate text " + // manca ,
//"palotipo text, " +
//"altezzapalo text" +
")";
db.execSQL(query);
inizialzza(db);
}
public void inizialzza(SQLiteDatabase db)
{
ContentValues contentValues = new ContentValues();
contentValues.put("coordinate", "ciao");
db.insert("tab", null, contentValues);
}
}
,例外是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mattia.provaconfragment, PID: 7045
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.mattia.provaconfragment.Database.DbManager.setProvider(DbManager.java:26)
at com.example.mattia.provaconfragment.PrimoFragment$3$1.onMapLongClick(PrimoFragment.java:107)
at com.google.android.gms.maps.GoogleMap$24.onMapLongClick(Unknown Source)
at com.google.android.gms.maps.internal.zzs$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:387)
at zr.a(:com.google.android.gms.DynamiteModulesB:93)
at maps.D.p$2.a(Unknown Source)
at maps.V.u.f(Unknown Source)
at maps.V.P.onLongPress(Unknown Source)
at maps.z.e$b.onLongPress(Unknown Source)
at maps.z.c.c(Unknown Source)
at maps.z.c$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)