0

我正在制作这个类来获取用户的当前位置并将其与数据库中的经度和纬度进行比较,它给了我空指针异常,有人可以帮我吗?我只想与纬度进行比较,我想获得 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
4

3 回答 3

2

您尚未初始化newHotels变量。在方法的开头添加以下行onCreate

newHotels = new ArrayList<Hotel>();
于 2012-06-22T16:58:10.040 回答
1

您可能还需要在使用前检查类别

String category = i.getStringExtra("category");

if(category.equals("hotels"))

注意类别可能为空,因此您最好执行以下操作:

if(category != null && category.equals("hotels"))
于 2012-06-22T17:13:45.803 回答
1

您可以通过查找包含您“知道”的类的行来在 LogCat 中找到您的错误。例如,在您的 LogCat 中有一行“在 egypt.interfaceAct.locationFinder.onCreate(locationFinder.java:47)”

在 LogCat 的所有类中,这是最知名的类,因此您可以检查第 47 行的类 locationFinder 并找出第 47 行的问题。

我假设您的 List newHotels 仍然为空。所以只需初始化 newsHotels

newHotels = new ArrayList<Hotel>(); 

在 onCreate 中添加此代码

于 2012-06-22T19:51:12.057 回答