2017-08-24 29 views
-1

幫助!我將地理位置數據從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(); 
       } 
    } }); } } 

回答

0

getWritableDatabase()是在SQLiteOpenHelper的方法。您代碼中的this指的是不是SQLiteOpenHelper的匿名內部類實例,也沒有此類方法。

你需要實現自己的類,擴展SQLiteOpenHelper,實例化它,然後調用實例上的getWritableDatabase()以獲得要使用的SQLiteDatabase

+0

你能舉個例子嗎?我不太明白。 –