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
Using java Syntax Highlighting
- package org.spargonaut.and.MapsActivity;
- // imports for use with google maps api
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapView;
- import com.google.android.maps.MyLocationOverlay;
- import com.google.android.maps.MapController;
- // imports for graphics
- import android.graphics.drawable.Drawable;
- import com.google.android.maps.ItemizedOverlay;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.OverlayItem;
- // for logging purposes
- import android.util.Log;
- // autocreated imports
- import android.app.Activity;
- import android.os.Bundle;
- public class MapsActivity extends MapActivity {
- /** global class members */
- MapView mvMap;
- MyLocationOverlay mMyLocationOverlay;
- // testing
- int lat1 = 1;
- int lon1 = 1;
- GeoPoint geod = new GeoPoint( lat1, lon1 );
- long testLat = 0;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // hmm, lets get some sort of mapview
- mvMap = ( MapView ) findViewById( R.id.mapmain );
- // get the controller
- final MapController mapController = mvMap.getController();
- // testing
- testLat = geod.getLatitudeE6();
- // lets add an overlay shall we?
- mMyLocationOverlay = new MyLocationOverlay( this, mvMap );
- mMyLocationOverlay.enableMyLocation();
- // testing
- // geod = mMyLocationOverlay.getMyLocation();
- // testLat = geod.getLatitudeE6();
- mMyLocationOverlay.runOnFirstFix(
- new Runnable() {
- public void run() {
- mapController.animateTo( mMyLocationOverlay.getMyLocation() );
- // testing
- // mapController.animateTo( new GeoPoint( 32748131, -96826517 ) );
- mapController.setZoom( 16 );
- }
- } );
- // testing
- geod = mMyLocationOverlay.getMyLocation();
- // testLat = geod.getLatitudeE6();
- // create an icon to use on the map
- Drawable marker = getResources().getDrawable( R.drawable.android_tiny_image );
- marker.setBounds( 0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight() );
- mvMap.getOverlays().add( new TestMapOverlay( marker ) );
- mvMap.setClickable( true );
- mvMap.setEnabled( true );
- mvMap.setSatellite( false );
- mvMap.setTraffic( false );
- mvMap.setStreetView( false );
- // start out with a general zoom
- mapController.setZoom( 16 );
- mvMap.invalidate();
- // testing
- int lat1 = 32867727;
- int lon1 = -96770478;
- // lets try to head over to my location
- try {
- // testing
- // mapController.animateTo( new GeoPoint( lat1, lon1 ) );
- mapController.animateTo( mMyLocationOverlay.getMyLocation() );
- } catch( Exception e ) {
- Log.i( "maptest1", "unable to goto my location:->" + e);
- }
- } // end onCreate()
- // to make the compiler shut up
- @Override
- public boolean isRouteDisplayed() { return false; }
- /* overlay stuff
- * this class is mainly stolen verbatim from MJAndroid.Microjobs.java
- */
- private class TestMapOverlay extends ItemizedOverlay< OverlayItem > {
- // marker the push-pin
- public TestMapOverlay( Drawable marker ) {
- super( marker );
- populate();
- } // end constructor
- /* see com.google.android.maps.ItemizedOverlay#size()
- */
- @Override
- public int size() {
- int size = 1;
- return size;
- }
- /* see com.google.android.maps.ItemizedOverlay#createItem( int )
- */
- @Override
- protected OverlayItem createItem( int i ) {
- //JobDetailCurser c = db.getJobDetails( i + 1 );
- //String contactName = c.getColContactName();
- //String description = c.getCOlDescription();
- //int lat = ( int ) c.getColLatitude();
- //int lon = ( int ) c.getLongitude();
- String contactName = "Contact: spargo";
- String description = "spargo's test description";
- // test location
- // int lat = 32748131;
- // int lon = -96826517;
- int lat = 32867727;
- int lon = -96770478;
- // northpark
- return new OverlayItem( new GeoPoint( lat, lon ), contactName, description );
- }
- /* MicroJobs has an onTap method that probably should be implemented,
- * but I'm not going to implement it at the moment
- * I prolly should just to learn wtf it does
- */
- } // end class TestMapOverlay()
- } // end class MapsActivity()
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.


