java.lang.RuntimeException:无法启动活动ComponentInfo:java.lang.NullPointerException

时间:2013-08-12 12:23:26

标签: android exception nullpointerexception runtime runtimeexception

我有一个 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

3 个答案:

答案 0 :(得分:1)

location = locationManager.getLastKnownLocation(provider);
如果没有可用的上一个已知位置,则不保证

返回实际结果。因此,nullpointerexception。

答案 1 :(得分:0)

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

try {
    editTextLat = (EditText) findViewById(R.id.editText1);
} catch (Exception e) {
    Log.e("MyTag", "Error accessing view editText1", e);
}

答案 2 :(得分:0)

我认为你需要这一行:

locationManager.requestLocationUpdates(provider, 2000, 10,
            mLocationListener);
在此之前

location = locationManager.getLastKnownLocation(provider);

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