从LocationListener类将位置数据写入SQLite数据库时出现Android错误

从LocationListener类将位置数据写入SQLite数据库时出现Android错误,android,sqlite,service,locationlistener,android-context,Android,Sqlite,Service,Locationlistener,Android Context,我有一个应用程序,其中我有一个实现locationlistener的服务类。我希望能够将onLocationChanged()中接收到的位置传递回我的主活动。到目前为止,我一直在试图通过写入SQLite数据库来实现这一点,但在尝试打开数据库以使其可写时出现了错误。我相信这与没有写作背景有关,但我一直没有弄明白。但是,当我在onCreate()中写入数据库时,它工作得非常好。我最初只是尝试这样做: @Override public void onLocationChanged(Locati

我有一个应用程序,其中我有一个实现locationlistener的服务类。我希望能够将onLocationChanged()中接收到的位置传递回我的主活动。到目前为止,我一直在试图通过写入SQLite数据库来实现这一点,但在尝试打开数据库以使其可写时出现了错误。我相信这与没有写作背景有关,但我一直没有弄明白。但是,当我在onCreate()中写入数据库时,它工作得非常好。我最初只是尝试这样做:

@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();
}
但这没有用,每次在db.openToWrite()行上都会给我一个nullPointerException。getApplicationContext()在这里也不起作用

我现在将其修改为在线程中写入DB,如下所示:

@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();     
        }
    }
我在onCreate()方法中将数据库初始化为:

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);
    }
但现在,第二次尝试不断给我一个更为精简的错误,但在我试图打开数据库以使其可写的那一行仍然出现错误。行MyLocListener.java:92引用行db.openToWrite()

错误是:

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)

解决了——如果其他人也有同样的问题,我已经找到了解决办法,我无法相信它有多简单。由于java的垃圾收集,我丢失了到SqlDatabase的链接。解决方案只是将其声明为静态。然后,我可以在onLocationChanged()方法中使用Sql insert/write方法。希望有帮助

您可以显示所有内容,但不显示错误所在的位置。向我们展示您的dbadapter,尤其是openToWrite方法。嘿,巴拉克,对此感到抱歉。我刚刚添加了整个dbAdapter类以及特定的openToWrite()方法。你对可能出现的问题有什么想法吗?谢谢