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" />

日志猫

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
4

1 回答 1

1

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

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

于 2012-08-01T12:13:09.007 回答