如何以有效的方式将GPS参数存储到数据库?

时间:2013-02-19 12:33:05

标签: multithreading sqlite gps

我正在尝试制作一个将GPS参数存储到SQLite数据库的应用程序。

我在这一行搜索了帖子,我终于设法通过SharedPreferences将参数(在String类型中)传递给主活动,这很完美,我可以将位置保存到数据库,但只需手动。我想在每次位置更改时在DB中插入一行,所以我开始尝试直接从OnLocationChanged方法访问DB的新代码,但程序在开始时崩溃,我不知道错误在哪里。这是代码:

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

1 个答案:

答案 0 :(得分:0)

我解决了。我在OnLocationchanged方法中实例化了db,以便程序可以工作:

 [..]

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

                [..]
          }
        [..]
        }

}