我想獲取用戶的位置並將其與我的數據庫中的位置進行比較,這是我提出的代碼,但它仍然無法正常工作,它給了我這個空指針異常行:SQLite查詢和LocationListener中的NullPointerException
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)
你從來沒有實例化數據庫...,你應該從SQLiteOpenHelper獲得SQLiteDatabase,你實例化SQLiteOpen Helper,並返回數據庫getWritableDatabase() – DGomez
這就是爲什麼你的遊標返回null,並調用moveToNext()引發NPE。 – Guardanis
對不起,你能更清楚嗎?我應該添加什麼代碼? – MahaK