我正在制作这个类来获取用户的当前位置并将其与数据库中的经度和纬度进行比较,它给了我空指针异常,有人可以帮我吗?我只想与纬度进行比较,我想获得 50 范围内的所有对象。
public class locationFinder extends ListActivity implements LocationListener {
EgyptDataSource datasource;
Location locObject;
String loc;
double lat;
double longi;
List<Integer> indexes;
List<Hotel> newHotels = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Intent i = getIntent();
String category = i.getStringExtra("category");
if(category.equals("hotels"))
{
List<Hotel> Latvalues = datasource.getAllHotelsLat();
float[] distance = new float[Latvalues.size()];
for (int j = 0; j < Latvalues.size(); j++) {
Location.distanceBetween(lat, longi, Latvalues.get(j).toDoubleLat(), 0.0, distance);
if(distance[0] <50)
{
indexes.add(j);
}
}
List<Hotel> allHotels = datasource.getAllHotels();
for (int q = 0; q < allHotels.size(); q++) {
for (int w = 0; w < indexes.size(); w++) {
if(q == indexes.get(w))
{
newHotels.add(allHotels.get(q));
}
}
}
if (newHotels.isEmpty())
{
Toast.makeText(this, "No results found!", Toast.LENGTH_LONG).show();
Intent i1 = new Intent(locationFinder.this, hotelSearch.class);
startActivity(i1);
}
ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this, android.R.layout.simple_list_item_1, newHotels);
setListAdapter(adapter);
}
}
public void onLocationChanged(Location location) {
lat = location.getLatitude();
longi = location.getLongitude();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
这是 LogCat
06-22 18:29:04.261: E/AndroidRuntime(30183): FATAL EXCEPTION: main
06-22 18:29:04.261: E/AndroidRuntime(30183): java.lang.RuntimeException: Unable to start activity ComponentInfo{egypt.interfaceAct/egypt.interfaceAct.locationFinder}: java.lang.NullPointerException
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.os.Looper.loop(Looper.java:143)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.main(ActivityThread.java:4914)
06-22 18:29:04.261: E/AndroidRuntime(30183): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 18:29:04.261: E/AndroidRuntime(30183): at java.lang.reflect.Method.invoke(Method.java:521)
06-22 18:29:04.261: E/AndroidRuntime(30183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-22 18:29:04.261: E/AndroidRuntime(30183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-22 18:29:04.261: E/AndroidRuntime(30183): at dalvik.system.NativeStart.main(Native Method)
06-22 18:29:04.261: E/AndroidRuntime(30183): Caused by: java.lang.NullPointerException
06-22 18:29:04.261: E/AndroidRuntime(30183): at egypt.interfaceAct.locationFinder.onCreate(locationFinder.java:47)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
06-22 18:29:04.261: E/AndroidRuntime(30183): ... 11 more