我已经通过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
}
}
你可以帮我吗???
答案 0 :(得分:1)
在调用locationManager.requestLocationUpdates之前,您是否初始化了locationManager?
在GPS
构造函数中,您可能需要尝试
locationManager = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
答案 1 :(得分:0)
您正在对locationManager执行操作,然后检查它是否为null:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if(locationManager!=null){
问题是locationManager是null。您可以在if语句中移动它,如下所示:
if(locationManager!=null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
另外,你需要在某个地方找到位置管理员,正如Selecsosi所说:
locationManager = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);