如何向数据库添加值?

时间:2015-07-15 02:10:06

标签: android database

编辑: LocationService类:

public class LocationService extends Service {
    private Location mLocation;
    private LocationDatabaseHelper mLocationDatabaseHelper;
    private LocationModel mLocationModel;
    public static final long NOTIFY_INTERVAL = 30 * 1000; 

    private Handler mHandler = new Handler();

    private Timer mTimer = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        // cancel if already existed
        if(mTimer != null) {
            mTimer.cancel();
        } else {
            // recreate new
            mTimer = new Timer();
        }
        // schedule task
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);

        mLocationDatabaseHelper = new LocationDatabaseHelper(this);


    }

    @Override
    public void onDestroy() {
        mTimer.cancel();
    }

    private class TimeDisplayTimerTask extends TimerTask implements LocationListener {

        @Override
        public void run() {

            mHandler.post(new Runnable() {

                @Override
                public void run() {

                    //Get GPS location
                    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    Criteria criteria = new Criteria();
                    String provider = locationManager.getBestProvider(criteria, true);
                    mLocation = locationManager.getLastKnownLocation(provider);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, TimeDisplayTimerTask.this);
                    mLocationModel = LocationModel.getInstance();

                    mLocationModel.setLng((float)mLocation.getLatitude());
                    mLocationModel.setLng((float)mLocation.getLongitude());

                    mLocationDatabaseHelper.insertGPSLogToDatabase(mLocationModel);
                }


            });
        }

LocationModel类:

public class LocationModel {
    private float mLat;
    private float mLng;
    private String mAddress;
    private int mStatus;
    private static LocationModel mLocation = null;


    private LocationModel(){

    }

    public static LocationModel getInstance() {
          if(mLocation == null) {
              mLocation = new LocationModel();
          }
          return mLocation;
       }

    public LocationModel(int mStatus,float mLat,
            float mLng,String mAddress){

        this.mLat = mLat;
        this.mLng = mLng;
        this.mStatus = mStatus;
    }
    public float getLat() {
        return mLat;
    }
    public float getLng() {
        return mLng;
    }
    public int getStatus() {
        return mStatus;
    }
    public void setLat(float lat) {
        this.mLat = lat;
    }
    public void setLng(float lng) {
        this.mLng = lng;
    }
    public void setStatus(int status) {
        this.mStatus = status;
    }

}

将值插入数据库的方法:

public boolean insertGPSLogToDatabase(LocationModel modelGPS){
        ContentValues values = new ContentValues();
        values.put("status", modelGPS.getStatus());
        values.put("lat", modelGPS.getLat());
        values.put("lng",modelGPS.getLng());    
        myDataBase.insert("locations", null, values);
        return true;        
    }

我想知道我可以将哪个LocationModel对象放到该方法中?如果我这样说:

mLocationModel = new LocationModel(null,mLocation.getLatitude(), 
                            mLocation.getLongitude());

它将显示错误,因为status是int stype且不能为null。如果我这样说:

 mLocationModel = new LocationModel();
 mLocationModel.setLat(mLocation.getLatitude());
 mLocationModel.setLng(mLocation.getLongitude());

我不知道它是对还是错,因为我在第34行获得NullPointer; mLocationModel = new LocationModel();"我是新手程序员,所以请教我好。谢谢!

已编辑:错误日志:

07-15 02:35:41.359: E/AndroidRuntime(1554): FATAL EXCEPTION: main
07-15 02:35:41.359: E/AndroidRuntime(1554): java.lang.NullPointerException
07-15 02:35:41.359: E/AndroidRuntime(1554):     at com.example.testservice.LocationService$TimeDisplayTimerTask$1.run(LocationService.java:77)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at android.os.Handler.handleCallback(Handler.java:615)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at android.os.Looper.loop(Looper.java:137)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-15 02:35:41.359: E/AndroidRuntime(1554):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

问题是您的位置为空。无论何时使用getLastKnownLocation()方法,都应该对结果进行空检查,因为它通常会返回一个空位置。

您还可以考虑使用onLocationChanged()回调,因为它永远不会给出空位置。

private class TimeDisplayTimerTask extends TimerTask implements LocationListener {

        @Override
        public void run() {

            mHandler.post(new Runnable() {

                @Override
                public void run() {

                    //Get GPS location
                    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    Criteria criteria = new Criteria();
                    String provider = locationManager.getBestProvider(criteria, true);
                    mLocation = locationManager.getLastKnownLocation(provider);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, TimeDisplayTimerTask.this);
                    mLocationModel = LocationModel.getInstance();

                    //add a null check here:
                    if (mLocation != null) {
                        mLocationModel.setLng((float) mLocation.getLatitude());
                        mLocationModel.setLng((float) mLocation.getLongitude());

                        mLocationDatabaseHelper.insertGPSLogToDatabase(mLocationModel);
                    }
                }


            });
        }

        @Override
        public void onLocationChanged(Location location) {

            //location will never be null here
            mLocation = location;
            mLocationModel = LocationModel.getInstance();

            mLocationModel.setLng((float) mLocation.getLatitude());
            mLocationModel.setLng((float) mLocation.getLongitude());

            mLocationDatabaseHelper.insertGPSLogToDatabase(mLocationModel);
        }
    }