您好,我目前正在尝试检索我当前的纬度和经度并将它们转换为地址,但是每次运行程序时我都会遇到应用程序崩溃。知道可能是什么原因造成的吗?
活动:
public class LocatahActivity extends Activity {
LocationManager lm;
String towers;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
towers = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(towers);
//double lat = (location.getLatitude()*1E6);
//double longi = (location.getLongitude()*1E6);
double lat = location.getLatitude();
double longi = location.getLongitude();
Log.e("TAG", "Latitude: " + lat);
Log.e("TAG", "Longitude: " + longi);
TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
TextView myAddress = (TextView)findViewById(R.id.myaddress);
myLatitude.setText("Latitude: " + String.valueOf(lat));
myLongitude.setText("Longitude: " + String.valueOf(longi));
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(lat, longi, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
myAddress.setText(sb.toString());
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Cannont get Address!");
}
}
}
显现:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
日志猫
08-01 19:20:45.572: E/AndroidRuntime(6233): FATAL EXCEPTION: main
08-01 19:20:45.572: E/AndroidRuntime(6233): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.location/com.example.location.LocatahActivity}: java.lang.NullPointerException
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.os.Looper.loop(Looper.java:137)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.ActivityThread.main(ActivityThread.java:4511)
08-01 19:20:45.572: E/AndroidRuntime(6233): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 19:20:45.572: E/AndroidRuntime(6233): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 19:20:45.572: E/AndroidRuntime(6233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
08-01 19:20:45.572: E/AndroidRuntime(6233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
08-01 19:20:45.572: E/AndroidRuntime(6233): at dalvik.system.NativeStart.main(Native Method)
08-01 19:20:45.572: E/AndroidRuntime(6233): Caused by: java.lang.NullPointerException
08-01 19:20:45.572: E/AndroidRuntime(6233): at com.example.location.LocatahActivity.onCreate(LocatahActivity.java:44)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.Activity.performCreate(Activity.java:4470)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-01 19:20:45.572: E/AndroidRuntime(6233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
08-01 19:20:45.572: E/AndroidRuntime(6233): ... 11 more