0
你好,我目前正在嘗試檢索我目前的經度和經度,並將它們轉換成地址,但我得到一個應用程序崩潰,每當我運行該程序。任何想法可能會造成這種情況?獲取應用程序崩潰時檢索經度和緯度
活動:
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" />
logcat的
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
你能提供一個logcat嗎? – Robert 2012-08-01 11:18:24
哎呀,不好意思忘了logcat,已經更新了這個帖子 – dythe 2012-08-01 11:21:39
請提供logcat – 2012-08-01 11:21:49