I am using LocationManager to convert from lat,longi to get the city name, but I am getting wrong results, I think there is something wrong in this code here:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
Log.d("msgh","msgh");
} else {
System.out.println("location not available");
Log.d("msg","msg");
}
the code doesn't enter the if statement and always enters the else statement.
after Editing, this is the logCat:
11-24 15:06:25.072: E/AndroidRuntime(6963): FATAL EXCEPTION: main
11-24 15:06:25.072: E/AndroidRuntime(6963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.survivingwithandroid.weatherapp/com.survivingwithandroid.weatherapp.MainActivity}: java.lang.NullPointerException
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.os.Looper.loop(Looper.java:137)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-24 15:06:25.072: E/AndroidRuntime(6963): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 15:06:25.072: E/AndroidRuntime(6963): at java.lang.reflect.Method.invoke(Method.java:525)
11-24 15:06:25.072: E/AndroidRuntime(6963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-24 15:06:25.072: E/AndroidRuntime(6963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 15:06:25.072: E/AndroidRuntime(6963): at dalvik.system.NativeStart.main(Native Method)
11-24 15:06:25.072: E/AndroidRuntime(6963): Caused by: java.lang.NullPointerException
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.location.Geocoder.<init>(Geocoder.java:83)
11-24 15:06:25.072: E/AndroidRuntime(6963): at com.survivingwithandroid.weatherapp.MainActivity.onCreate(MainActivity.java:227)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.Activity.performCreate(Activity.java:5133)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-24 15:06:25.072: E/AndroidRuntime(6963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)