检索纬度和经度时应用程序崩溃

时间:2012-08-01 11:12:11

标签: android gps location latitude-longitude

您好我正在尝试检索当前的纬度和经度并将其转换为地址,但每次运行程序时我都会遇到应用程序崩溃。知道可能导致这种情况的原因吗?

的活动:

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

1 个答案:

答案 0 :(得分:1)

您的location对象似乎为空。当您使用getLastKnownLocation()时,可能会发生这种情况。如果没有最后位置,或者当前禁用了提供程序,则使用此方法将获得null。

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