2012-08-01 85 views
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 
+0

你能提供一个logcat吗? – Robert 2012-08-01 11:18:24

+0

哎呀,不好意思忘了logcat,已经更新了这个帖子 – dythe 2012-08-01 11:21:39

+0

请提供logcat – 2012-08-01 11:21:49

回答

1

好像你location对象为null。这可能是因为您正在使用getLastKnownLocation()。如果没有最后一个位置,或者提供程序当前被禁用,您将使用此方法得到一个空值。

除此之外,你说你想要当前位置。这是获取当前位置的错误方法。要获取用户的当前位置,您需要使用LocationManager API中给出的requestLocationUpdates()方法。使用这些和一个LocationListener接收更新。请记住,在获得位置之前,它可能需要几秒到几分钟的时间。