我已经通过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
}
}
你能帮我吗???