Error entering Android data into SQLite database from LocationListener class

I have an application in which I have a service class that implements the locationlistener function. I want to be able to pass the location obtained in onLocationChanged () back to my main activity. I have tried to implement this so far by writing a SQLite database, but have been getting errors when trying to open the database for writing. I believe this is due to the lack of a recording context, but I could not figure it out. However, writing to the database works fine when I do this in onCreate (). At first I just tried to do it like:

@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 useless and gave me a nullPointerException in the db.openToWrite () line every time. getApplicationContext () does not work either.

Now I changed it to write to the database in the stream 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 gives me a much more concise error, but it is still to blame for the line where I am trying to open the database for writing. The string MyLocListener.java:92 refers to the string db.openToWrite ();

Error:

 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) 
+4
source share
1 answer

SOLVED --- In case someone has a different problem, I figured out a solution, and I cannot believe how simple it was. Due to java garbage collection, I lost contact with my SqlDatabase. The solution was simply to declare it static. Then I can use the Sql input / write methods inside my onLocationChanged () method. Hope this helps!

+3
source

Source: https://habr.com/ru/post/1415261/


All Articles