Tommy Devoy
Tommy Devoy

Reputation: 13549

Android Error Writing Location Data to SQLite Database from LocationListener Class

I have an application wherein I have a service class that implements a locationlistener. I want to be able to pass the location received in onLocationChanged() back to my main activity. I've been trying to implement this so far by writing to a SQLite Database, but have been getting errors when trying to open to database to be writeable. I believe it has something to do with not having the write context, but I haven't been able to figure it out. However, writing to the database works perfectly fine when I do it in onCreate(). I initially just tried doing it as:

@Override
    public void onLocationChanged(Location loc) {//Spits out single location for current disconnect
        System.out.println("location equals "+loc);
        latitude=Double.toString(loc.getLatitude());
        longitude=Double.toString(loc.getLongitude());
        writeToDb(MyLocListener.this,latitude,longitude);       
        man.removeUpdates(listener);//Stops location manager from listening for updates
        listener=null;
        man=null;
    }

public void writeToDb(Context context,String latitude,String longitude){
    db=new DbAdapter(context);
    db.openToWrite();
    db.deleteAll();
    db.insert(latitude);
    db.insert(longitude);
    db.close();
}

But that was no use, and would give me a nullPointerException on the db.openToWrite() line everytime. getApplicationContext() does not work here either.

I've now modified it to write to the DB in a thread as:

@Override
public void onLocationChanged(Location loc) {//Spits out single location for current disconnect
    System.out.println("location equals "+loc);
    latitude=Double.toString(loc.getLatitude());
    longitude=Double.toString(loc.getLongitude());
    Runnable runner=new SaveLocation(latitude,longitude);
    new Thread(runner).start();
    man.removeUpdates(listener);//Stops location manager from listening for updates
    listener=null;
    man=null;
}

public class SaveLocation implements Runnable{
        String latitude;
        String longitude;
    //  DbAdapter db;
        public SaveLocation(String latitude,String longitude){
            this.latitude=latitude;
            this.longitude=longitude;

        }
        @Override
        public void run() {
            db.openToWrite();
            db.deleteAll();
            db.insert(latitude);
            db.insert(longitude);
            db.close();     
        }
    }

And I initialized the Database in the onCreate() method as:

public class MyLocListener extends Service implements LocationListener{ 
    static LocationManager man;
    static LocationListener listener;
    static Location location;
    public DbAdapter db;

    @Override
    public void onCreate() {
        super.onCreate();   
        this.db=new DbAdapter(MyLocListener.this);
    }

But now, this second attempt keeps giving me a much more condensed error, but it still faults on the line where i attempt to open the database to be writeable. The line MyLocListener.java:92 refers to the line db.openToWrite();

The error is:

05-30 15:35:19.698: W/dalvikvm(4557): threadid=9: thread exiting with uncaught exception (group=0x4001d7e0)
05-30 15:35:19.706: E/AndroidRuntime(4557): FATAL EXCEPTION: Thread-10
05-30 15:35:19.706: E/AndroidRuntime(4557): java.lang.NullPointerException
05-30 15:35:19.706: E/AndroidRuntime(4557):     at com.phonehalo.proto.MyLocListener$SaveLocation.run(MyLocListener.java:92)
05-30 15:35:19.706: E/AndroidRuntime(4557):     at java.lang.Thread.run(Thread.java:1096)

Upvotes: 4

Views: 604

Answers (1)

Tommy Devoy
Tommy Devoy

Reputation: 13549

SOLVED--- In case anyone else is having this same problem, I have figured out the solution and I cannot believe how simple it was. Due to java's garbage collection, I was losing the link to my SqlDatabase. The solution was simply to declare it as static. I can then use the Sql insert/write methods inside of my onLocationChanged() method. Hope it helps!

Upvotes: 3

Related Questions