null pointer exception from getMyLocation()

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

null pointer exception from getMyLocation()

Postby spargonaut » Thu Apr 29, 2010 6:19 pm

hello everyone.

I'm trying to create a simple mapActivity that only places a little android over a particular coordinate and animates the map to that point. I've determined that I'm getting a null pointer exception whenever i try to execute the line:

mapController.animateTo( mMyLocationOverlay.getMyLocation() );

the above line is within a try/catch block and I used the eclipse debugger to nail down where I'm getting that error from. I'm testing this on my actual phone so ( I'm assuming ) I don't need to create mock locations. If i'm wrong about that, please correct me.

In my manifest I have the lines:

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


here is my code if you would like to have a look. if it matters, I'm following the example laid out in the MJAndroid project that is described in O'Reilly's "Android Application Development" book.

I'm new to android development, so if you see any bad habits or have any other constructive suggestions, please feel free to speak up.

thanks
spargonaut

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.spargonaut.and.MapsActivity;
  3.  
  4.  
  5.  
  6. // imports for use with google maps api
  7.  
  8. import com.google.android.maps.MapActivity;
  9.  
  10. import com.google.android.maps.MapView;
  11.  
  12. import com.google.android.maps.MyLocationOverlay;
  13.  
  14. import com.google.android.maps.MapController;
  15.  
  16.  
  17.  
  18.  
  19.  
  20. // imports for graphics
  21.  
  22. import android.graphics.drawable.Drawable;
  23.  
  24. import com.google.android.maps.ItemizedOverlay;
  25.  
  26. import com.google.android.maps.GeoPoint;
  27.  
  28. import com.google.android.maps.OverlayItem;
  29.  
  30.  
  31.  
  32. // for logging purposes
  33.  
  34. import android.util.Log;
  35.  
  36.  
  37.  
  38. // autocreated imports
  39.  
  40. import android.app.Activity;
  41.  
  42. import android.os.Bundle;
  43.  
  44.  
  45.  
  46. public class MapsActivity extends MapActivity {
  47.  
  48.        
  49.  
  50.         /** global class members */
  51.  
  52.         MapView mvMap;
  53.  
  54.         MyLocationOverlay mMyLocationOverlay;
  55.  
  56.        
  57.  
  58.         // testing
  59.  
  60.         int lat1 = 1;
  61.  
  62.         int lon1 = 1;
  63.  
  64.         GeoPoint geod = new GeoPoint( lat1, lon1 );
  65.  
  66.         long testLat = 0;
  67.  
  68.        
  69.  
  70.     /** Called when the activity is first created. */
  71.  
  72.     @Override
  73.  
  74.     public void onCreate(Bundle savedInstanceState) {
  75.  
  76.         super.onCreate(savedInstanceState);
  77.  
  78.         setContentView(R.layout.main);
  79.  
  80.        
  81.  
  82.         // hmm, lets get some sort of mapview
  83.  
  84.         mvMap = ( MapView ) findViewById( R.id.mapmain );
  85.  
  86.        
  87.  
  88.         // get the controller
  89.  
  90.         final MapController mapController = mvMap.getController();
  91.  
  92.        
  93.  
  94.         // testing
  95.  
  96.         testLat = geod.getLatitudeE6();
  97.  
  98.        
  99.  
  100.         // lets add an overlay shall we?
  101.  
  102.         mMyLocationOverlay = new MyLocationOverlay( this, mvMap );
  103.  
  104.         mMyLocationOverlay.enableMyLocation();
  105.  
  106.        
  107.  
  108.         // testing
  109.  
  110. //        geod = mMyLocationOverlay.getMyLocation();
  111.  
  112. //        testLat = geod.getLatitudeE6();
  113.  
  114.        
  115.  
  116.         mMyLocationOverlay.runOnFirstFix(
  117.  
  118.                         new Runnable() {
  119.  
  120.                                 public void run() {
  121.  
  122.                                         mapController.animateTo( mMyLocationOverlay.getMyLocation() );
  123.  
  124.                                        
  125.  
  126.                                         // testing
  127.  
  128. //                                      mapController.animateTo( new GeoPoint( 32748131, -96826517 ) );
  129.  
  130.                                         mapController.setZoom( 16 );
  131.  
  132.                                 }
  133.  
  134.                         } );
  135.  
  136.        
  137.  
  138.         // testing
  139.  
  140.       geod = mMyLocationOverlay.getMyLocation();
  141.  
  142. //      testLat = geod.getLatitudeE6();
  143.  
  144.  
  145.  
  146.        
  147.  
  148.         // create an icon to use on the map
  149.  
  150.         Drawable marker = getResources().getDrawable( R.drawable.android_tiny_image );
  151.  
  152.         marker.setBounds( 0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight() );
  153.  
  154.         mvMap.getOverlays().add( new TestMapOverlay( marker ) );
  155.  
  156.        
  157.  
  158.         mvMap.setClickable( true );
  159.  
  160.         mvMap.setEnabled( true );
  161.  
  162.        
  163.  
  164.         mvMap.setSatellite( false );
  165.  
  166.         mvMap.setTraffic( false );
  167.  
  168.         mvMap.setStreetView( false );
  169.  
  170.        
  171.  
  172.         // start out with a general zoom
  173.  
  174.         mapController.setZoom( 16 );
  175.  
  176.         mvMap.invalidate();
  177.  
  178.  
  179.  
  180.         // testing
  181.  
  182.                 int lat1 = 32867727;
  183.  
  184.                 int lon1 = -96770478;
  185.  
  186.        
  187.  
  188.         // lets try to head over to my location
  189.  
  190.         try {
  191.  
  192.                 // testing
  193.  
  194. //              mapController.animateTo( new GeoPoint( lat1, lon1 ) );
  195.  
  196.                
  197.  
  198.                 mapController.animateTo( mMyLocationOverlay.getMyLocation() );
  199.  
  200.         } catch( Exception e ) {
  201.  
  202.                 Log.i( "maptest1", "unable to goto my location:->" + e);
  203.  
  204.         }
  205.  
  206.        
  207.  
  208.        
  209.  
  210.     } // end onCreate()
  211.  
  212.    
  213.  
  214.     // to make the compiler shut up
  215.  
  216.     @Override
  217.  
  218.     public boolean isRouteDisplayed() { return false; }
  219.  
  220.    
  221.  
  222.     /* overlay stuff
  223.  
  224.      * this class is mainly stolen verbatim from MJAndroid.Microjobs.java
  225.  
  226.      */
  227.  
  228.     private class TestMapOverlay extends ItemizedOverlay< OverlayItem > {
  229.  
  230.        
  231.  
  232.         // marker the push-pin
  233.  
  234.         public TestMapOverlay( Drawable marker ) {
  235.  
  236.                 super( marker );
  237.  
  238.                 populate();
  239.  
  240.         } // end constructor
  241.  
  242.        
  243.  
  244.         /* see com.google.android.maps.ItemizedOverlay#size()
  245.  
  246.          */
  247.  
  248.         @Override
  249.  
  250.         public int size() {
  251.  
  252.                 int size = 1;
  253.  
  254.                 return size;
  255.  
  256.         }
  257.  
  258.        
  259.  
  260.         /* see com.google.android.maps.ItemizedOverlay#createItem( int )
  261.  
  262.          */
  263.  
  264.         @Override
  265.  
  266.         protected OverlayItem createItem( int i ) {
  267.  
  268.                 //JobDetailCurser c = db.getJobDetails( i + 1 );
  269.  
  270.                 //String contactName = c.getColContactName();
  271.  
  272.                 //String description = c.getCOlDescription();
  273.  
  274.                 //int lat = ( int ) c.getColLatitude();
  275.  
  276.                 //int lon = ( int ) c.getLongitude();
  277.  
  278.                
  279.  
  280.                 String contactName = "Contact: spargo";
  281.  
  282.                 String description = "spargo's test description";
  283.  
  284.  
  285.  
  286.                 // test location
  287.  
  288. //              int lat = 32748131;
  289.  
  290. //              int lon = -96826517;
  291.  
  292.                 int lat = 32867727;
  293.  
  294.                 int lon = -96770478;
  295.  
  296.                 // northpark
  297.  
  298.                 return new OverlayItem( new GeoPoint( lat, lon ), contactName, description );
  299.  
  300.         }
  301.  
  302.        
  303.  
  304.         /* MicroJobs has an onTap method that probably should be implemented,
  305.  
  306.          * but I'm not going to implement it at the moment
  307.  
  308.          * I prolly should just to learn wtf it does
  309.  
  310.          */
  311.  
  312.        
  313.  
  314.     } // end class TestMapOverlay()
  315.  
  316.    
  317.  
  318. } // end class MapsActivity()
  319.  
  320.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4



also, if you look at the following line in the debugger ( the one after mMyLocationOverlay.runOnFirstFix() ):

// testing
geod = mMyLocationOverlay.getMyLocation();

you'll see that getMyLocation returns null.
spargonaut
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 29, 2010 6:02 pm
Location: dallas, tx

Top

Postby kekomal » Thu Apr 29, 2010 10:07 pm

Try...

mvMap.getOverlays().add(mMyLocationOverlay);

...before any...

mMyLocationOverlay.getMyLocation()
kekomal
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Mar 17, 2010 10:44 am
Location: Bilbao, Spain

Postby spargonaut » Thu Apr 29, 2010 10:28 pm

werd!

thanks for the suggestion! I've been racking my brain over this for the past few days. as soon as i have the chance i'll try it out and let you know how it works.

do I only need to do this once, or should it be done every time i call *.getMyLocation()?
spargonaut
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 29, 2010 6:02 pm
Location: dallas, tx

Postby spargonaut » Thu Apr 29, 2010 10:56 pm

unfortunately, its still doing it ( returning null from mvMyLocationOverlay.getMyLocation() ).

I tried it in two places...

first below this line:
mMyLocationOverlay.enableMyLocation();

and above this line:
// testing
geod = mMyLocationOverlay.getMyLocation();

so that it reads:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mMyLocationOverlay.enableMyLocation();
  3.  
  4.  
  5.  
  6. mvMap.getOverlays().add( mMyLocationOverlay );
  7.  
  8.        
  9.  
  10.            // testing
  11.  
  12. //        geod = mMyLocationOverlay.getMyLocation();
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


and the second time just before the try/catch block so that it reads...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //              int lat1 = 32.867727;
  3.  
  4. //              int lon1 = -96.770478;
  5.  
  6.        
  7.  
  8.         // addev suggestion 1 - kekomal
  9.  
  10.       mvMap.getOverlays().add( mMyLocationOverlay );
  11.  
  12.      
  13.  
  14.       geod = mMyLocationOverlay.getMyLocation();
  15.  
  16.  
  17.  
  18.        
  19.  
  20.         // lets try to head over to my location
  21.  
  22.         try {
  23.  
  24.                 // testing
  25.  
  26. //              mapController.animateTo( new GeoPoint( lat1, lon1 ) );
  27.  
  28.                
  29.  
  30.                 mapController.animateTo( mMyLocationOverlay.getMyLocation() );
  31.  
  32.         } catch( Exception e ) {
  33.  
  34.                 Log.i( "maptest1", "unable to goto my location:->" + e);
  35.  
  36.         }
  37.  
  38.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


both times, geod is showing as null in the debugger and the map that is pulling up is nowhere near where it should be.

thanks for the suggestion though. I'm open to trying others if you or anyone else has them.
spargonaut
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 29, 2010 6:02 pm
Location: dallas, tx

Postby kekomal » Thu Apr 29, 2010 11:27 pm

My suggestion was only needed to be done once (after the enableMyLocation would be a good place).

Try including also the permission android.permission.ACCESS_FINE_LOCATION. Test the mMyLocationOverlay.getMyLocation() inside the Runnable before the others.
kekomal
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Mar 17, 2010 10:44 am
Location: Bilbao, Spain

Postby spargonaut » Thu Apr 29, 2010 11:50 pm

Added the permission you suggested.
and added the line you suggested after the first instance of mMyLocationOverlay.enableMyLocation() and uncommented the line that reads: geod = mMyLocationOverlay.getMyLocation();
and its still returning null.

Also, I tried putting a breakpoint within the runnable() but eclipse seemed to just jump right over it.

I'm noticing in LogCat the line:
04-29 11:37:33.220: ERROR/AndroidRuntime(122): ERROR: thread attach failed

not sure if that helps, but i get the feeling that for some reason the runnable isn't executing properly.

anything else to try?

thanks again!
spargonaut
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 29, 2010 6:02 pm
Location: dallas, tx

Top

Postby spargonaut » Fri Apr 30, 2010 12:06 am

wait wait wait wait!!!

I take that back. its actually working!
its taking a while, but its working!!!

well, geod is still resolving to null in the debugger, but when i run the application without the debugger, it actually puts a little blue dot over my location and puts my little android dude next to it.

thanks a ton!

have a great day or night or whatever time it is where you are!

( although, upon further reflection, I wonder if it was adding the permission, the *.add() method or both that got it to work )

thoughts?
spargonaut
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 29, 2010 6:02 pm
Location: dallas, tx

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests