0

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

我已经搜索了这一行中的帖子,最后我设法通过 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
4

1 回答 1

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

                [..]
          }
        [..]
        }

}
于 2013-02-25T12:58:05.257 回答