0

I got a java.lang.NullPointerException.

I can't find the problem.

Log attached below.

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();
        }

    }
}

Here is a log.

criteria.setAccuracy(Criteria.NO_REQUIREMENT);

This line makes a runtime exception.

After I delete this line,

I also got a runtime exception at 'updateLocation(location.getLatitude(), location.getLongitude()); '

How can i fix it?!

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
7
  • What is your line No.62 in GPSActivity.java Commented Aug 12, 2013 at 12:27
  • Which line is GPSActivity.java:62 ? Commented Aug 12, 2013 at 12:27
  • did you give required permissions ? Commented Aug 12, 2013 at 12:27
  • NO 62 line is criteria.setAccuracy(Criteria.NO_REQUIREMENT); Commented Aug 12, 2013 at 12:28
  • I give permission android.permission.INTERNET, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION. more permission is needed? Commented Aug 12, 2013 at 12:30

3 Answers 3

1
location = locationManager.getLastKnownLocation(provider);

is not guaranteed to return an actual result if there is no last known location available. Hence the nullpointerexception.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to have this line:

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

before this one:

location = locationManager.getLastKnownLocation(provider);

As far as I know you can't get location info unless the LocationListener is working and finding out your position.

Comments

0

If the problem is not the fact the location might be returned then this error normally points to the fact you might be trying to access a view, with a valid ID, but is not actually in the current layout. Maybe try surrounding with a try/catch to figure out which view is causing the trouble.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.