0

我得到了一个java.lang.NullPointerException。

我找不到问题。

日志附在下面。

public class GPSActivity extends MapActivity {

    Location location;
    EditText editTextLat, editTexLng, editTextAddress;
    // overlay
    Geocoder coder;
    MapView mapview;
    MapController controller; 
    MyItemizedOverlay itemizedOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps_layout);
        mapview = (MapView) findViewById(R.id.mapview);
        controller = mapview.getController(); 
        controller.setZoom(16);
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapview.getOverlays();
        listOfOverlays.add(mapOverlay); 
        Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
        itemizedOverlay = new MyItemizedOverlay(drawable);
        listOfOverlays.add(itemizedOverlay); 
        coder = new Geocoder(this, Locale.KOREA);
        editTextLat = (EditText) findViewById(R.id.editText1);
        editTexLng = (EditText) findViewById(R.id.editText2);
        editTextAddress = (EditText) findViewById(R.id.editText3); 

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);
        updateLocation(location.getLatitude(), location.getLongitude());
        locationManager.requestLocationUpdates(provider, 2000, 10,
                mLocationListener);                         }

    private void updateLocation(double lat, double lng) {
        String sLocationInfo = "";
        if (location != null) {
            editTextLat.setText(String.valueOf(lat));
            editTexLng.setText(String.valueOf(lng));
            GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1e6));
            controller.animateTo(point);
            OverlayItem overlayItem = new OverlayItem(point, null, null);
            itemizedOverlay.addOverlay(overlayItem);            try {
                List<Address> addresses = coder.getFromLocation(lat, lng, 1);
                if (addresses != null) {
                    Address addr = addresses.get(0);
                    for (int i = 0; i <= addr.getMaxAddressLineIndex(); i++) {
                        String addLine = addr.getAddressLine(i);
                        sLocationInfo += String.format("%s", addLine);
                    }
                }
            } catch (IOException e) {
                Toast.makeText(getBaseContext(), "not found", Toast.LENGTH_SHORT).show();
            }
            location.setLatitude(lat);
            location.setLongitude(lng);
        } else {
            sLocationInfo = "cant found location";
        }
        editTextAddress.setText(sLocationInfo);
    }

    LocationListener mLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) { // TODO
                                                            // Auto-generated
                                                            // method stub
            updateLocation(location.getLatitude(), location.getLongitude());
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        public void onProviderEnabled(String provider) { // TODO Auto-generated
                                                            // method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    };

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    class MapOverlay extends Overlay {
        @Override
        public boolean onTouchEvent(MotionEvent e, MapView mapView) {
            // TODO Auto-generated method stub
            if (e.getAction() == 1) {
                GeoPoint point = mapView.getProjection().fromPixels(
                        (int) e.getX(), (int) e.getY());
                updateLocation(point.getLatitudeE6() / 1E6,
                        point.getLongitudeE6() / 1E6);
            }
            return false;
        }
    }

    class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
        private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

        public MyItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
            // TODO Auto-generated constructor stub
        }

        @Override
        protected OverlayItem createItem(int i) {
            // TODO Auto-generated method stub
            return mOverlays.get(i);
        }

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return mOverlays.size();
        }

        public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }

    }
}

这是一个日志。

criteria.setAccuracy(Criteria.NO_REQUIREMENT);

此行产生运行时异常。

删除此行后,

updateLocation(location.getLatitude(), location.getLongitude()); 我在 ' '处也遇到了运行时异常

我该如何解决?!

08-12 11:21:34.760: E/AndroidRuntime(2120): FATAL EXCEPTION: main
08-12 11:21:34.760: E/AndroidRuntime(2120): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lg.icou/com.lg.icou.GPSActivity}: java.lang.NullPointerException
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.os.Looper.loop(Looper.java:137)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at java.lang.reflect.Method.invoke(Method.java:511)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at dalvik.system.NativeStart.main(Native Method)
08-12 11:21:34.760: E/AndroidRuntime(2120): Caused by: java.lang.NullPointerException
08-12 11:21:34.760: E/AndroidRuntime(2120):     at com.lg.icou.GPSActivity.onCreate(GPSActivity.java:62)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.Activity.performCreate(Activity.java:5104)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-12 11:21:34.760: E/AndroidRuntime(2120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-12 11:21:34.760: E/AndroidRuntime(2120):     ... 11 more
4

3 回答 3

1
location = locationManager.getLastKnownLocation(provider);

如果没有可用的最后一个已知位置,则不保证返回实际结果。因此出现空指针异常。

于 2013-08-12T12:27:54.147 回答
0

我认为你需要有这条线:

locationManager.requestLocationUpdates(provider, 2000, 10,
            mLocationListener);

在此之前:

location = locationManager.getLastKnownLocation(provider);

据我所知,除非 LocationListener 正在工作并找出您的位置,否则您无法获取位置信息。

于 2013-08-12T12:39:11.327 回答
0

如果问题不是位置可能被返回,那么此错误通常表明您可能正试图访问具有有效 ID 的视图,但实际上不在当前布局中。也许尝试使用 try/catch 来找出导致问题的视图。

try {
    editTextLat = (EditText) findViewById(R.id.editText1);
} catch (Exception e) {
    Log.e("MyTag", "Error accessing view editText1", e);
}
于 2013-08-12T12:29:17.453 回答