0

我想获取用户位置并将其与我的数据库中的位置进行比较,这是我想出的代码,但它仍然无法正常工作,它在这一行中给了我空指针异常:

Cursor cursor = database.query(MySQLiteHelper.TABLE_Hotel,
            new String[]{MySQLiteHelper.H_ID, MySQLiteHelper.H_Name, MySQLiteHelper.H_Longitude, MySQLiteHelper.H_Latitude}, null, null, null, null, null);

这是整个班级:

public class locationFinder extends ListActivity implements LocationListener {


    List<Hotel> hotelsInRange = new ArrayList<Hotel>();
    MySQLiteHelper mySQL;
    SQLiteDatabase database;
    EgyptDataSource datasource;
    Location locObject;
    String loc;
    double lat;
    double longi;
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
            }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        setListOfHotelsInRange(location);
    }


    private void setListOfHotelsInRange(Location userLocation)
    {
        Cursor cursor = database.query(MySQLiteHelper.TABLE_Hotel,
                new String[]{MySQLiteHelper.H_ID, MySQLiteHelper.H_Name, MySQLiteHelper.H_Longitude, MySQLiteHelper.H_Latitude}, null, null, null, null, null);


        while(cursor.moveToNext())
        {
            double longi = cursor.getDouble(cursor.getColumnIndex(MySQLiteHelper.H_Longitude));
            double lat = cursor.getDouble(cursor.getColumnIndex(MySQLiteHelper.H_Latitude));

            Location currentHotelLocation = new Location("Current Hotel");
            currentHotelLocation.setLatitude(lat);
            currentHotelLocation.setLongitude(longi);

            double distanceInMeters = userLocation.distanceTo(currentHotelLocation);

            if (distanceInMeters < 500000000)
            {
                //hotel is in range
                Hotel hotel = new Hotel();
                hotel.set_id( cursor.getInt(0));
                hotel.setName(cursor.getString(0));
                hotelsInRange.add(hotel);
            }

        }
        ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this,
                android.R.layout.simple_list_item_1, hotelsInRange);
                setListAdapter(adapter);

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

这是 LogCat:

06-22 22:55:32.984: E/AndroidRuntime(2378): FATAL EXCEPTION: main
06-22 22:55:32.984: E/AndroidRuntime(2378): java.lang.NullPointerException
06-22 22:55:32.984: E/AndroidRuntime(2378):     at egypt.interfaceAct.locationFinder.setListOfHotelsInRange(locationFinder.java:99)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at egypt.interfaceAct.locationFinder.onLocationChanged(locationFinder.java:93)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at android.os.Looper.loop(Looper.java:143)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at android.app.ActivityThread.main(ActivityThread.java:4914)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at java.lang.reflect.Method.invokeNative(Native Method)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at java.lang.reflect.Method.invoke(Method.java:521)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-22 22:55:32.984: E/AndroidRuntime(2378):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

该成员database永远不会被初始化。你应该在你onCreate()locationFinder活动中初始化它。就像@DGomez 提到的

public void onCreate(Bundle icicle){
  super.onCreate(icicle);

  mySQL = new MySQLiteHelper();
  database = mySQL.getWriteableDatabase();

  LocationManager locManager = (LocationManager)  etSystemService(Context.LOCATION_SERVICE);
  locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
于 2012-06-22T21:31:32.803 回答
0

作为 DGomez,您没有获得用于查询的数据库实例。

假设MySQLiteHelper扩展SQLiteOpenHelper做如下的事情......

onCreate(...)方法...

mySQL = new MySQLiteHelper(this, "my-database-name", null, 1);

然后在setListOfHotelsInRange(...)您查询数据库之前的方法中...

database = mySQL.getWriteableDatabase();
于 2012-06-22T21:31:57.780 回答