SQLiteDatabase dbm = this.getWritableDatabase();递归

时间:2017-08-24 13:45:03

标签: android sqlite getwritabledatabase

帮助!我将地理位置数据从android传输到数据库。给出错误消息。错误:(45,46)错误:找不到符号方法getWritableDatabase()。救命! enter image description here

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

1 个答案:

答案 0 :(得分:0)

getWritableDatabase()SQLiteOpenHelper中的一种方法。代码中的this引用的View.OnClickListener内部类实例不是SQLiteOpenHelper,也没有这样的方法。

您需要实现自己的扩展SQLiteOpenHelper的类,对其进行实例化,然后在实例上调用getWritableDatabase()以获取要使用的SQLiteDatabase