Search code examples
androiddictionarygeolocationsamsung-mobilesony

Loading Map crashes on few devices


I have a listview and onItemSelected, will call an Activity to get user location (his current lat and lng), this activity has no layout, then this activity automatically calls another activity which gets lat and lng of the item selected from the listview and marks both the user location and item location on the map.

This worked fine when I was testing on Emulator and Samsung Galaxy Ace, But it crashes when I'm testing on Micromax A50 and Sony Xperia.

Sad thing is that I can't install drivers of the later 2 devices, so I cant get logcat.
Can any 1 guess what might have gone wrong??

My code for finding user location is:

public class MyLocationActivity extends Activity implements LocationListener {
    private LocationManager mgr;
    private String best;
    public static double myLocationLatitude;
    public static double myLocationLongitude;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);

        dumpProviders();

        Criteria criteria = new Criteria();

        best = mgr.getBestProvider(criteria, true);
        Log.d("best provider", best);

        Location location = mgr.getLastKnownLocation(best);
        dumpLocation(location);

        /*Intent intent = new Intent(MyLocationActivity.this, ShowMapActivity.class);
        startActivity(intent);*/

        Intent intent = new Intent(MyLocationActivity.this, MapMarkerActivity.class);
        startActivity(intent);
        finish();
    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        dumpLocation(location);

    }

    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 void onPause() {

        super.onPause();
        mgr.removeUpdates(this);
    }

    @Override
    protected void onResume() {

        super.onResume();
        mgr.requestLocationUpdates(best, 15000, 1, this);
    }

    private void dumpLocation(Location l) {

        if (l == null) {

            myLocationLatitude = 0.0;
            myLocationLongitude = 0.0;
        } else {

            myLocationLatitude = l.getLatitude();
            myLocationLongitude = l.getLongitude();
        }
    }

    private void dumpProviders() {

        List<String> providers = mgr.getAllProviders();
        for (String p : providers) {

            dumpProviders(p);
        }
    }

    private void dumpProviders(String s) {

        LocationProvider info = mgr.getProvider(s);
        StringBuilder builder = new StringBuilder();
        builder.append("name: ").append(info.getName());
    }

}

and item location is:

public class MapMarkerActivity extends MapActivity {

    private MapView map = null;
    private MyLocationOverlay me = null;
    private Drawable marker1;
    private Drawable marker2;

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

        Button feedButton = (Button) findViewById(R.id.feedButton_particularEntry);
        feedButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                Intent intent = new Intent(MapMarkerActivity.this,
                        FeedListViewActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
            }
        });

        Button iWantButton = (Button) findViewById(R.id.iWantButton_particularEntry);
        iWantButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(MapMarkerActivity.this,
                        IWantActivity.class);
                startActivity(intent);
            }
        });

        Button shareButton = (Button) findViewById(R.id.shareButton_particularEntry);
        shareButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(MapMarkerActivity.this,
                        ShareActivity.class);
                startActivity(intent);
            }
        });

        Button profileButton = (Button) findViewById(R.id.profileButton_particularEntry);
        profileButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(MapMarkerActivity.this,
                        ProfileActivity.class);
                startActivity(intent);
            }
        });

        map = (MapView) findViewById(R.id.map);

        map.getController().setCenter(
                getPoint(FeedListViewActivity.lat, FeedListViewActivity.lng));
        map.getController().setZoom(13);
        map.setBuiltInZoomControls(true);

        marker1 = getResources().getDrawable(R.drawable.marker2);

        marker2 = getResources().getDrawable(R.drawable.marker1);

        marker1.setBounds(0, 0, marker1.getIntrinsicWidth(),
                marker1.getIntrinsicHeight());
        marker2.setBounds(0, 0, marker1.getIntrinsicWidth(),
                marker1.getIntrinsicHeight());

        map.getOverlays().add(new SitesOverlay(marker1));

        me = new MyLocationOverlay(this, map);
        map.getOverlays().add(me);
    }

    @Override
    public void onResume() {
        super.onResume();

        me.enableCompass();
    }

    @Override
    public void onPause() {
        super.onPause();

        me.disableCompass();
    }

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_S) {
            map.setSatellite(!map.isSatellite());
            return (true);
        } else if (keyCode == KeyEvent.KEYCODE_Z) {
            map.displayZoomControls(true);
            return (true);
        }

        return (super.onKeyDown(keyCode, event));
    }

    private GeoPoint getPoint(double lat, double lon) {
        return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
    }

    public class SitesOverlay extends ItemizedOverlay<OverlayItem> {
        private List<OverlayItem> items = new ArrayList<OverlayItem>();

        public SitesOverlay(Drawable marker) {
            super(marker);

            boundCenterBottom(marker);

            OverlayItem oli1 = new OverlayItem(getPoint(
                    MyLocationActivity.myLocationLatitude,
                    MyLocationActivity.myLocationLongitude), "YL",
                    "Your Location");
            oli1.setMarker(marker2);
            items.add(oli1);

            OverlayItem oli2 = new OverlayItem(getPoint(
                    FeedListViewActivity.lat, FeedListViewActivity.lng), "SL",
                    "Store Location");
            oli2.setMarker(marker1);
            items.add(oli2);

            populate();
        }

        @Override
        protected OverlayItem createItem(int i) {
            return (items.get(i));
        }

        @Override
        protected boolean onTap(int i) {
            Toast.makeText(MapMarkerActivity.this, items.get(i).getSnippet(),
                    Toast.LENGTH_SHORT).show();

            return (true);
        }

        @Override
        public int size() {
            return (items.size());
        }
    }
}

Solution

  • Maybe this can help you: Location Method Calls Crashes On Some Devices