1

I am trying to implement the map showing the current location with the path. while running it crashes.

I checked the manifest too.. but I dont know what the error is..

PLease help me, I am new bee in android..

here is my code:

MapActivity.java

package com.GoogleMaps;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */

 private MapView mapView;
 private LocationManager lm;
 private LocationListener ll;
 private MapController mc;
 GeoPoint p = null;
 Drawable defaultMarker = null;

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

    mapView = (MapView)findViewById(R.id.mapview);
    //show zoom in/out buttons
    mapView.setBuiltInZoomControls(true);
    //Standard view of the map(map/sat)
    mapView.setSatellite(false);
    //get controller of the map for zooming in/out
    mc = mapView.getController();
    // Zoom Level
    mc.setZoom(18); 

    // MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
    List<Overlay> list = mapView.getOverlays();
   //  list.add(myLocationOverlay);

    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    ll = new MyLocationListener();

    lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0,
            0,
            ll);

    //Get the current location in start-up
    GeoPoint initGeoPoint = new GeoPoint(
           (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000),
           (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));
           mc.animateTo(initGeoPoint);
}

protected class MyLocationOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            Paint paint = new Paint();

            super.draw(canvas, mapView, shadow);
            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point myScreenCoords = new Point();

            mapView.getProjection().toPixels(p, myScreenCoords);

            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

            canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
            canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
            return true;
        }
    }

private class MyLocationListener implements LocationListener{

      public void onLocationChanged(Location argLocation) {
       // TODO Auto-generated method stub
       GeoPoint myGeoPoint = new GeoPoint((int)(argLocation.getLatitude()*1000000), (int)(argLocation.getLongitude()*1000000));

       Toast.makeText(getBaseContext(),
               "New location latitude [" +argLocation.getLatitude() +
               "] longitude [" + argLocation.getLongitude()+"]",
               Toast.LENGTH_SHORT).show();


       mc.animateTo(myGeoPoint);

      }

      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
      }
     }    
protected boolean isRouteDisplayed() {
    return false;
}
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   > 
 <LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   >
    <TextView
     android:id="@+id/longitude"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="Longitude:"
  />
  <TextView
   android:id="@+id/latitude"
   android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="Latitude:"
  />
 </LinearLayout>
 <com.google.android.maps.MapView
  android:id="@+id/mapview"
  android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="false"
  android:apiKey="0dB02TjP-H_UqgsDpfxAhtsvGo5Z8cUJsTeHeDA"
 />
</LinearLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.GoogleMaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library  android:required="true" android:name="com.google.android.maps"/>
        <activity
            android:label="@string/app_name"
            android:name=".MapsActivity" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
  <uses-library android:name="com.google.android.maps" />
   </application>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-sdk android:minSdkVersion="8" />
</manifest>

logcat file:

11-29 03:26:49.620: D/AndroidRuntime(671): Shutting down VM
11-29 03:26:49.620: W/dalvikvm(671): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-29 03:26:49.640: E/AndroidRuntime(671): FATAL EXCEPTION: main
11-29 03:26:49.640: E/AndroidRuntime(671): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.GoogleMaps/com.GoogleMaps.MapsActivity}: java.lang.NullPointerException
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.os.Looper.loop(Looper.java:123)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 03:26:49.640: E/AndroidRuntime(671):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 03:26:49.640: E/AndroidRuntime(671):  at java.lang.reflect.Method.invoke(Method.java:521)
11-29 03:26:49.640: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-29 03:26:49.640: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-29 03:26:49.640: E/AndroidRuntime(671):  at dalvik.system.NativeStart.main(Native Method)
11-29 03:26:49.640: E/AndroidRuntime(671): Caused by: java.lang.NullPointerException
11-29 03:26:49.640: E/AndroidRuntime(671):  at com.GoogleMaps.MapsActivity.onCreate(MapsActivity.java:65)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-29 03:26:49.640: E/AndroidRuntime(671):  ... 11 more
11-29 03:26:52.320: I/Process(671): Sending signal. PID: 671 SIG: 9
skynet
  • 9,898
  • 5
  • 43
  • 52
Gagandeep
  • 43
  • 1
  • 8
  • Please post the exception stack trace from LogCat – skynet Nov 29 '11 at 03:47
  • Exception is nullpointer exception which occurs at line 65.Since it occurs that the object value is null while trying to use null value, nullpointer exception will occur. May be it occur at taking object for Geopoint or next line mc.animateTo(initGeoPoint); please check those lines.... – deepa Nov 29 '11 at 04:14

1 Answers1

1

You are getting a NullPointerException at line 65 of the MapsActivity class. Could you post that line all by itself?

A possible culprit is that you do not check whether getLastLocation returns null, which is a possibility.

Surround those lines with a null check

//Get the current location in start-up, if it exists
if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
    GeoPoint initGeoPoint = new GeoPoint(
        (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000),
        (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));
     mc.animateTo(initGeoPoint);
}
skynet
  • 9,898
  • 5
  • 43
  • 52
  • GeoPoint initGeoPoint = new GeoPoint( (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000), (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000)); – Gagandeep Nov 29 '11 at 04:17
  • Yes, so `getLastLocation` is null. You will have to handle that case – skynet Nov 29 '11 at 04:19
  • how will i do that:s can u edit the code?? i am not getting how will i implement that – Gagandeep Nov 29 '11 at 04:21
  • it is still crashing.. i ran the file :s – Gagandeep Nov 29 '11 at 04:31
  • Did the stack trace change? What lines did you change? – skynet Nov 29 '11 at 04:31
  • That is because there was no previous location, thus it returned null in the first place. You have to request location updates, and there is plenty of information on SO on how to do that – skynet Nov 29 '11 at 04:41
  • Whats a SO? SORRY never heard about this :s – Gagandeep Nov 29 '11 at 04:43
  • This site is StackOverflow... SO – skynet Nov 29 '11 at 04:45
  • locationManager.requestLocationUpdates() i should just add this line in my code? – Gagandeep Nov 29 '11 at 04:46
  • can you please help me.. sorry if i am been really a bugging guy :$ I really need to learn all this :$ – Gagandeep Nov 29 '11 at 04:51
  • 1
    This is a good place to start http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a as well as searching around. If you have more trouble post a new question as this is a digression from your original. – skynet Nov 29 '11 at 04:55
  • k boss thanks alot :D i will give you comments up and also accept thsi answer.. :) – Gagandeep Nov 29 '11 at 04:58