ユーザーの場所を取得してデータベース内の場所と比較したいのですが、これは私が思いついたコードですが、まだ機能していません。次の行でnullポインター例外が発生します。
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)