2013-02-19 49 views
0

我試圖做一個應用程序來存儲GPS參數到SQLite數據庫。如何以有效的方式將GPS參數存儲到數據庫?

我已經搜索了這一行的帖子,並且我最終設法通過SharedPreferences將參數(以字符串類型)傳遞給主要活動,這非常完美,我可以將位置保存到數據庫,但只是手動。我想在每次更改位置時在數據庫中插入一行,因此我開始嘗試直接從OnLocationChanged方法訪問數據庫的新代碼,但在開始時程序崩潰,我不知道錯誤在哪裏。這是代碼:

package com.example.trackinggps; 
import com.example.trackinggps.GPSSQLiteHelper; 
import android.os.Bundle;[..]//Hiden 

public class MainActivity extends Activity { 

    public Button Close; 
    public int num=0; 
    public LocationManager locationManager=null;  

    GPSSQLiteHelper usdbh = new GPSSQLiteHelper(this, "GPSTracking", null, 1); 
    final SQLiteDatabase db = usdbh.getWritableDatabase(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(db!=null){ 
      Close = (Button) findViewById(R.id.button4); 

      final LocationUpdateHandler lUH = new LocationUpdateHandler(); 

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1500,0, lUH);  

      Close.setOnClickListener(new OnClickListener(){ [..]//Impl. Hiden 
      Close.setOnLongClickListener(new OnLongClickListener(){ [..]//Impl. Hiden 
     } 
    } 


    public class LocationUpdateHandler implements LocationListener { 

     @Override 
     public void onLocationChanged(Location loc) { 
      String lat = (String) Double.toString(loc.getLatitude()); 
      String lng = (String) Double.toString(loc.getLongitude()); 
      String acc = (String) Float.toString(loc.getAccuracy()); 
      String spd= (String) Float.toString(loc.getSpeed()); 
      String time=(String) Long.toString(loc.getTime()); 

      if(db!=null){ 
       db.execSQL("INSERT INTO GPSTracking (Id, Time, Longitude, Latitude, Accuracy, Speed) " +"VALUES ("+ num +", '" + 
        time + "' , '" + lat + "' , '" + lng + "' , '" + acc + "' , '" + spd + "')"); 

       num++; 
      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) {} 
     @Override 
     public void onProviderEnabled(String provider) {} 
     @Override 
     public void onStatusChanged(String provider, int status, 
       Bundle extras) {} 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu){ [..]//Impl. Hiden 
} 

的logcat的結果,是這樣的一個:

Shutting down VM 
threadid=1: thread exiting with uncaught exception (group=0x40014760) 
FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.trackinggps/com.example.trackinggps.MainActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:126) 
    at android.app.ActivityThread.main(ActivityThread.java:3997) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:491) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149) 
    at com.example.trackinggps.MainActivity.<init>(MainActivity.java:28) 
    at java.lang.Class.newInstanceImpl(Native Method) 
    at java.lang.Class.newInstance(Class.java:1424) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1022) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
    ... 11 more 
+0

爲什麼不能從'OnLocationChanged'訪問數據庫? – 2013-02-19 13:13:37

+0

首先,我聲明瞭主活動的onCreate方法之外的SQLiteHelper和SQLiteDatabase,以便從LocationUpdateHandler訪問它們,但在虛擬設備中開始執行時會崩潰。所以我猜想,這是不可能的:/ – Burakito 2013-02-19 17:47:37

+0

...和logcat? – 2013-02-19 18:00:35

回答

0

我解決它。我已經實例化了OnLocationchanged方法中的數據庫,以便該程序工作:

[..] 

public class MainActivity extends Activity { 

[..] 


     GPSSQLiteHelper usdbh = new GPSSQLiteHelper(this, "GPSTracking", null, 1);   
     SQLiteDatabase db = null; 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      [..] 
     } 

     public class LocationUpdateHandler implements LocationListener { 

      @Override 
      public void onLocationChanged(Location loc) { 

      db = usdbh.getWritableDatabase(); 

       [..] 
      } 
     [..] 
     } 

} 
相關問題