我正在使用LocationManager从lat转换为longi以获取城市名称,但是我得到了错误的结果,我认为这里的代码有问题:
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");
}
代码不会进入if语句并始终输入else语句。
编辑后,这是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)
答案 0 :(得分:0)
您是否尝试过打开GPS来获取位置信息?根据{{3}},此方法返回:
提供程序的最后已知位置,或null
确保您的手机上有固定位置,然后再开始工作。此外,您可能希望the docs涵盖一个良好的策略,以便在有可用时获取用户位置,否则请求新用户位置。
希望它有所帮助。
答案 1 :(得分:0)
以下是获取位置的完整代码。此代码将自动为您提供最佳可用提供商(GPS或网络)。此外,它不会为您提供最后的已知位置,而是当前位置。
protected String currentLatitude, currentLongitude;
double lat, lon;
protected LocationManager locationManager;
private LocationListener ll;
locationManager = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this,
"GPS enabled. Finding fine location. ",
Toast.LENGTH_SHORT).show();
ll = new GpsListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, ll);
} else {
if (locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(this,
"GPS disabled. Finding coarse location.",
Toast.LENGTH_SHORT).show();
ll = new GpsListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, ll);
} else {
Toast.makeText(this,
"Enable location settings to get current location.",
Toast.LENGTH_LONG).show();
}
}
private class GpsListener implements LocationListener {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
try {
Toast.makeText(getApplicationContext(), "Location Found",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
lat = location.getLatitude();
lon = location.getLongitude();
Geocoder gcd = new Geocoder(context, Locale.getDefault());
try{
List<Address> addresses = gcd.getFromLocation(lat, lon, 1);
if (addresses.size() > 0)
String cityName=addresses.get(0).getLocality();
}
catch (IOException e) {}
catch (NullPointerException e) {}
currentLatitude = Double.toString(lat);
currentLongitude = Double.toString(lon);
try {
if (ll != null)
locationManager.removeUpdates(ll);
} catch (Exception e) {
}
locationManager = null;
} else {
Toast.makeText(getApplicationContext(), "No Location Found",
Toast.LENGTH_LONG).show();
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}// end of Class
编辑 - 请务必在Android Manifest中拥有以下权限:
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION