Ryan Scott
Ryan Scott

Reputation: 31

Attempting to launch Google Maps crashes my app

On my android app where I simply want the location to be displayed on the map when the user has clicked on the button. However when I click on the button the android emulator crashes.

Below is the code for the button:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.btn_map);
    btn.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {
            startActivity(new Intent("com337.assignment.whereami.DisplayMap"));
        }
    });

    Log.d(tag, "In the OnCreate() event");
}

The code when the map is launched is:

public class DisplayMap extends MapActivity 
{
MapView mapView;
MapController mc;
GeoPoint p;

private LocationManager lm;
private LocationListener locationListener;


protected class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
    {
        super.draw(canvas, mapView, shadow);

        //Convert the GeoPoint to screen Pixels
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        //add the marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin5);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
        return true;

    }


}

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    //navigate to a point - Belfast 
    mc = mapView.getController();
    String coordinates[] = {"54.6000", "5.9167"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint((int) (lat*1E6), (int) (lng*1E6));
    mc.animateTo(p);
    mc.setZoom(13);

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

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

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    //mapView.invalidate();
}

private class MyLocationListener implements LocationListener
{
    public void onLocationChanged(Location loc)
    {
        p = new GeoPoint((int)(loc.getLatitude() * 1E6), (int)(loc.getLongitude() * 1E6));

        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
        try
        {
            List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);

            String geo = "";
            if (addresses.size() > 0)
            {
                for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
                    geo += addresses.get(0).getAddressLine(i) + "\n";
            }
            Toast.makeText(getBaseContext(), geo, Toast.LENGTH_SHORT).show();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        mc.animateTo(p);
        mc.setZoom(18);
    }

    public void onProviderDisabled(String provider)
    {

    }

    public void onProviderEnabled(String provider)
    {

    }

    public void onStatusChanged(String provider, int status, Bundle excess)
    {

    }
}

//create a method so that when the user presses the volume up/down key the map zooms in/out
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    MapController mc = mapView.getController();
    switch(keyCode)
    {
    case KeyEvent.KEYCODE_VOLUME_UP:mc.zoomIn();
        break;
    case KeyEvent.KEYCODE_VOLUME_DOWN:mc.zoomOut();
        break;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean isRouteDisplayed() 
{

    return false;
}

}

The updated logcat info for the crash is:

12-13 16:40:49.231: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-13 16:40:49.331: E/AndroidRuntime(699): FATAL EXCEPTION: main
12-13 16:40:49.331: E/AndroidRuntime(699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com337.assignment.whereami/com337.assignment.whereami.DisplayMap}: java.lang.NullPointerException
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.os.Looper.loop(Looper.java:137)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-13 16:40:49.331: E/AndroidRuntime(699):  at java.lang.reflect.Method.invokeNative(Native Method)
12-13 16:40:49.331: E/AndroidRuntime(699):  at java.lang.reflect.Method.invoke(Method.java:511)
12-13 16:40:49.331: E/AndroidRuntime(699):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-13 16:40:49.331: E/AndroidRuntime(699):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-13 16:40:49.331: E/AndroidRuntime(699):  at dalvik.system.NativeStart.main(Native Method)
12-13 16:40:49.331: E/AndroidRuntime(699): Caused by: java.lang.NullPointerException
12-13 16:40:49.331: E/AndroidRuntime(699):  at com337.assignment.whereami.DisplayMap.onCreate(DisplayMap.java:66)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.Activity.performCreate(Activity.java:5008)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-13 16:40:49.331: E/AndroidRuntime(699):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-13 16:40:49.331: E/AndroidRuntime(699):  ... 11 more

Upvotes: 2

Views: 312

Answers (2)

dymmeh
dymmeh

Reputation: 22306

You are calling

startActivity(new Intent("com337.assignment.whereami.DisplayMap"));

Using new Intent("com337.assignment.whereami.DisplayMap"); implies that you want to create an intent with the ACTION of "com337.assignment.whereami.DisplayMap" .. Since this is a class name you are passing in and not an action no activities will be found. You aren't catching the ActivityNotFoundException so you will get a crash

Since you are obviously trying to launch this class you should not be doing this.. if this is a part of your project you should use

 startActivity(new Intent(YourActivityName.this, com337.assignment.whereami.DisplayMap.class));

or

startActivity(new Intent(getApplicationContext(), com337.assignment.whereami.DisplayMap.class));

You can read up on using intents here

Upvotes: 4

Julian Suarez
Julian Suarez

Reputation: 4521

Use this constructor:

Intent(Context packageContext, Class<?> cls)

Instead of the one you are using

Intent(String action)

Upvotes: 1

Related Questions