SQLiteDatabase dbm = this.getWritableDatabase();递归
问题描述:
帮助!我将地理位置数据从android传输到数据库。给出错误消息。错误:(45,46)错误:无法找到符号方法getWritableDatabase()。帮帮我! enter image description hereSQLiteDatabase dbm = this.getWritableDatabase();递归
public class AndroidGPSTrackingActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
SQLiteDatabase dbm = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("LAT_G",
gps.getLatitude());
cv.put("LANG", gps.getLongitude());
boolean result = dbm.insert("table name", cv);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
} }); } }
答
getWritableDatabase()
是在SQLiteOpenHelper
的方法。您代码中的this
指的是不是SQLiteOpenHelper
的匿名内部类实例,也没有此类方法。
你需要实现自己的类,扩展SQLiteOpenHelper
,实例化它,然后调用实例上的getWritableDatabase()
以获得要使用的SQLiteDatabase
。
你能举个例子吗?我不太明白。 –