1

我已经通过gps为请求位置创建了一个类,所以在我可以在我的活动中使用这个类来请求经度和纬度之后,但我总是这个错误,我不明白为什么......

我有这个logcat:

07-10 17:13:49.645: E/AndroidRuntime(10269): FATAL EXCEPTION: main
07-10 17:13:49.645: E/AndroidRuntime(10269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.provags/com.example.provags.MainActivity}: java.lang.NullPointerException
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.os.Looper.loop(Looper.java:137)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at java.lang.reflect.Method.invokeNative(Native Method)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at java.lang.reflect.Method.invoke(Method.java:511)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at dalvik.system.NativeStart.main(Native Method)
07-10 17:13:49.645: E/AndroidRuntime(10269): Caused by: java.lang.NullPointerException
07-10 17:13:49.645: E/AndroidRuntime(10269):    at com.example.provags.Gps.getLocation(Gps.java:19)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at com.example.provags.Gps.<init>(Gps.java:16)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at com.example.provags.MainActivity.onCreate(MainActivity.java:16)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.Activity.performCreate(Activity.java:4465)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-10 17:13:49.645: E/AndroidRuntime(10269):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-10 17:13:49.645: E/AndroidRuntime(10269):    ... 11 more

对于通过 gps 请求位置的方法。看起来一切都很好,但它不起作用:(

public class Gps implements LocationListener {
Context myContext;
Location posizione;
double latitudine,longitudine;
LocationManager locationManager;
public Gps(Context c){
    myContext=c;
    getLocation();
}
public Location getLocation(){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    if(locationManager!=null){
        posizione=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(posizione!=null){
            latitudine=posizione.getLatitude();
            longitudine=posizione.getLongitude();
        }
    }
    return posizione;
}

public double latitudine(){
    if(posizione!=null){
        latitudine=posizione.getLatitude();
    }
    return latitudine;
}
public double longitudine(){
    if(posizione!=null){
        longitudine=posizione.getLongitude();
    }
    return longitudine;
}
public void stopGps(){
    if (locationManager!=null){
        locationManager.removeUpdates(Gps.this);
    }
}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}
@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}
@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

你能帮我吗???

4

2 回答 2

1

你有没有在调用 locationManager.requestLocationUpdates 之前初始化 locationManager?

GPS构造函数中,您可能想尝试

locationManager = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
于 2013-07-10T15:47:23.787 回答
0

您正在对 locationManager 执行操作,然后检查它是否为空:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    if(locationManager!=null){

问题是 locationManager 为空。您可以像这样在 if 语句中移动它:

if(locationManager!=null){
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

正如 Selecsosi 所说,您实际上还需要在某个地方找到位置管理器:

locationManager = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
于 2013-07-10T15:47:52.000 回答