1

It keeps on getting this error for me and on the emulator it is showing force close. Not getting where it went wrong. Actually I am creating a program (with reference to a book) to get the location data. Following is my program and logcat error. I referred these link1, link2, but I couldn't find the solution from those. Can someone please suggest me what is the problem? Even I already added this in manifest

<uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and in the application tag

<uses-library android:name="com.google.android.maps"/>

and here is my .java file

public class LBSMap extends MapActivity {

    MapView mapView;
    MapController mc;
    GeoPoint p;

    LocationManager lm;
    LocationListener locationListener;

    private class MapOverlay extends com.google.android.maps.Overlay
    {
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
        {
            super.draw(canvas, mapView, shadow);
            Point screenPts=new Point();
            mapView.getProjection().toPixels(p, screenPts);

            Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50,null);
            return true;
        }

        public boolean onTouchEvent(MotionEvent event,MapView mapView)
        {
            if(event.getAction()==1)
            {
                GeoPoint p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());

            Geocoder geoCoder=new Geocoder(getBaseContext(),Locale.getDefault());
            try{
                List<Address> addresses=geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1);
                String add="";
                if(addresses.size()>0)
                {
                    for(int i=0;i<addresses.get(0).getMaxAddressLineIndex();i++)
                        add +=addresses.get(0).getAddressLine(i) + "\n";
                }
                Toast.makeText(getBaseContext(),add,Toast.LENGTH_SHORT).show();

            }catch(IOException e){
                e.printStackTrace();
            }
            return true;
            }
            return false;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lbsmap);

        mapView=(MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        mapView.setTraffic(true);

        mc=mapView.getController();

        Geocoder geoCoder=new Geocoder(this,Locale.getDefault());

        try{
            List<Address> addresses=geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1); //line 103
            String add="";
            if(addresses.size()>0)
            {
                for(int i=0;i<addresses.get(0).getMaxAddressLineIndex();i++)
                    add +=addresses.get(0).getAddressLine(i) + "\n";
            }
            Toast.makeText(getBaseContext(),add,Toast.LENGTH_SHORT).show();

        }catch(IOException e){
            e.printStackTrace();
        }

        MapOverlay mapOverlay=new MapOverlay();
        List<Overlay> listOfOverlays=mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();

        lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();
    }

    public void onResume()
    {
        super.onResume();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    public void onPause()
    {
        super.onPause();
        lm.removeUpdates(locationListener);
    }

    private class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location loc)
        {
            if(loc!=null){
                Toast.makeText(getBaseContext(), "Location changed : Lat: "+loc.getLatitude()+ "Lng: "+loc.getLongitude(), Toast.LENGTH_SHORT).show();
                p=new GeoPoint((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6));
                mc.animateTo(p);
                mc.setZoom(18);
            }

        }
        public void onProviderDisabled(String provider)
        {}
        public void onProviderEnabled(String provider)
        {}
        public void onStatusChanged(String provider, int status, Bundle extras)
        {}
    }


    @Override
    public boolean isRouteDisplayed()
    {
        return false;
    }
}

Below is the screenshot of the logcat.

enter image description here

Community
  • 1
  • 1
Spike
  • 147
  • 3
  • 17

2 Answers2

0

I'm guessing the GeoPoint p is null in your onCreate(), as when you assign a value to it in the Overlay code, you use

GeoPoint p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());

Which creates another local variable, instead of using the global one. Try using:

p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());

You may need to add a final modifier to the declaration to have it work inside another class.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks for your response. Tried this. Still showing the same error.:( – Spike Sep 21 '12 at 17:47
  • when I added sop before the line p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY()); It gives the value of p object as null. But why? – Spike Sep 21 '12 at 18:12
  • I'm guessing that since you assign p in onTouchEvent, it'll be null until you touch the map. – Raghav Sood Sep 21 '12 at 18:17
  • Yes. I too thought the same thing. But in the book it is given like that. When I initialize p to some values in the onCreate() method, it is working. BTW How come there is only one android chat room now and I am unable to enter that room. – Spike Sep 21 '12 at 18:25
  • Glad it's working. There are still a few Android rooms. You may not have access to the ones with diamonds next to them, and hence may not be able to talk. – Raghav Sood Sep 21 '12 at 18:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16976/discussion-between-anoop-kumar-and-raghav-sood) – Spike Sep 21 '12 at 18:45
0

Please check that you have internet access code for SDK version > 9

// Allow network access

    if (android.os.Build.VERSION.SDK_INT > 9) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

    }
Renan
  • 462
  • 7
  • 24