I’ve tried every example i could google, and every solution i could find on both stackoverflow and other sites.. But this i just cant get to work..
Now im using the example from androids own website where i create a seperate class. This is my code:
super.onCreate(savedInstanceState);
setContentView(R.layout.win_showmap) ;
MapView map = (MapView) findViewById(R.id.googlemap) ;
map.setBuiltInZoomControls(true);
mapController = map.getController() ;
mapController.setZoom(12);
int lat = (int)(Double.parseDouble(GlobalClass.pass_lattitude) * 1000000) ;
int lon = (int)( Double.parseDouble(GlobalClass.pass_longitude) * 1000000);
GeoPoint point = new GeoPoint(lat,lon) ;
p = point ;
mapController.animateTo(point) ;
//---Add a location marker---
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
Mapoverlays itemizedoverlay = new Mapoverlays(drawable);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
By using Log.d i can find the crash point that gives null point error, to be the line:
List mapOverlays = mapView.getOverlays();
My API key works fine, because i can se the map if i remove the 6 lines of mapoverlay code.
Don’t know if it means anything, but i get these Logcats when not having the overlay code.
02-03 22:44:05.892: I/MapActivity(2374): Handling network change notification:CONNECTED
02-03 22:44:05.892: E/MapActivity(2374): Couldn’t get connection factory client
And if i include the overlay code it crashes and i get the Logcat:
02-03 22:45:33.183: E/AndroidRuntime(2465): FATAL EXCEPTION: main
02-03 22:45:33.183: E/AndroidRuntime(2465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.closebuy.namespace/com.closebuy.namespace.Show_map}: java.lang.NullPointerException
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.os.Looper.loop(Looper.java:123)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 22:45:33.183: E/AndroidRuntime(2465): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 22:45:33.183: E/AndroidRuntime(2465): at java.lang.reflect.Method.invoke(Method.java:521)
02-03 22:45:33.183: E/AndroidRuntime(2465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 22:45:33.183: E/AndroidRuntime(2465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 22:45:33.183: E/AndroidRuntime(2465): at dalvik.system.NativeStart.main(Native Method)
02-03 22:45:33.183: E/AndroidRuntime(2465): Caused by: java.lang.NullPointerException
02-03 22:45:33.183: E/AndroidRuntime(2465): at com.closebuy.namespace.Show_map.onCreate(Show_map.java:48)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 22:45:33.183: E/AndroidRuntime(2465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
I’ve tried it on emulator and on my Galaxy S2.. Please help, i really wan’t this to work
Where are you declaring and initializing the variable
mapView? The onlyMapViewtype variable I see initialized ismap.So, I would try:
If that still crashes with a null pointer error, then you have no MapView view declared within the
win_showmap.xmllayout.