我想获取用户位置并将其与我数据库中的位置进行比较,这是我提出的代码,但它仍然无效,它在这一行中给出了空指针异常:
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)
答案 0 :(得分: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);
}
答案 1 :(得分:0)
作为DGomez,您没有获得要与查询一起使用的数据库实例。
假设MySQLiteHelper
延伸SQLiteOpenHelper
执行以下操作......
在onCreate(...)
方法中......
mySQL = new MySQLiteHelper(this, "my-database-name", null, 1);
然后在查询数据库之前在setListOfHotelsInRange(...)
方法中执行...
database = mySQL.getWriteableDatabase();