Я пытаюсь получить текущее местоположение в Google maps.But я получаю объект местоположения, как NPE.The код:Получение NPE в объекте местоположения в Google Maps
public void getcurrentlocation()
{
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
location = getLocation();
// Getting latitude of the current location
current_latitude=location.getLatitude();
LogCat:
07-16 10:42:13.335: E/AndroidRuntime(6340): FATAL EXCEPTION: main
07-16 10:42:13.335: E/AndroidRuntime(6340): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.igloo.storelocater/com.igloo.storelocater.MainActivity}: java.lang.NullPointerException
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.os.Looper.loop(Looper.java:130)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-16 10:42:13.335: E/AndroidRuntime(6340): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 10:42:13.335: E/AndroidRuntime(6340): at java.lang.reflect.Method.invoke(Method.java:507)
07-16 10:42:13.335: E/AndroidRuntime(6340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-16 10:42:13.335: E/AndroidRuntime(6340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-16 10:42:13.335: E/AndroidRuntime(6340): at dalvik.system.NativeStart.main(Native Method)
07-16 10:42:13.335: E/AndroidRuntime(6340): Caused by: java.lang.NullPointerException
07-16 10:42:13.335: E/AndroidRuntime(6340): at com.igloo.storelocater.MainActivity.getcurrentlocation(MainActivity.java:121)
07-16 10:42:13.335: E/AndroidRuntime(6340): at com.igloo.storelocater.MainActivity.onCreate(MainActivity.java:99)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-16 10:42:13.335: E/AndroidRuntime(6340): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-16 10:42:13.335: E/AndroidRuntime(6340): ... 11 more
Получение долготы текущего местоположения:
current_longitude= location.getLongitude();
}
public Location getLocation() {
try {
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
100L,
1.0f, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
current_latitude = location.getLatitude();
current_longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
100L,
1.0f, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
current_latitude = location.getLatitude();
current_longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
Второй метод я получил его от StackOverflow itself.But стил Я получаю объект местоположения как null. Пожалуйста, помогите.
отправить свой логарифм ... –