I am writing an application that retrieves coordinates from a webservice, then plots them on a map together with my current location: I am able to write the code wich does not have any error but when i run it on the emulator it crashes….what could be the problem: Here is the code:
public abstract class MapActivity extends com.google.android.maps.MapActivity{
// Instance attributes
private MapView mapView = null;
private MapController mc = null;
private GeoPoint p = null;
private LocationManager locationManager;
private LocationListener locationListener;
@Override
protected void onDestroy() {
super.onDestroy();
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
public void initMap(){
// Get the main element
mapView = (MapView) findViewById(R.id.mapView);
mc = mapView.getController();
// Add a zoom
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
// Initialize the zoom level
mc = mapView.getController();
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
public void drawElements(MapView mapView, Canvas canvas, String element){
if (mapView == null){
return;
}
try {
// Get elements
JSONObject elements = JSONfunctions.getJSONfromURL("http://10.0.2.2/android/rental.php");
JSONArray results = elements.getJSONArray("rental");
if (results != null && results.length() != 0){
for (int i = 0; i < elements.length(); i++) {
JSONObject result = results.getJSONObject(i);
//---fetch the coordinates from the remote server------
double lat = Double.parseDouble((String) result.get("lat"));
double lon = Double.parseDouble((String) result.get("lon"));
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
canvas.drawBitmap(bmp, screenPts.x-20, screenPts.y-40, null);
}
}
} catch (JSONException e) {
Log.e(null,"JSONException error");
}
}
/**
* Re-center the icon on the map
*/
public void refreshMapLocation(){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0, 0,locationListener);
}
private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
GeoPoint p = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
// Animate the map on the point
if (p != null && mc !=null){
mc.animateTo(p);
} else {
Log.e(null,"Couldn't refresh location on the map");
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
protected boolean isRouteDisplayed() {
return false;
}
/**
* Map overlay
*/
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);
if (p != null){
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
canvas.drawBitmap(bmp, screenPts.x-10, screenPts.y-20, null);
}
//drawElements(mapView,canvas,"Hospitals");
return true;
}
}
}
edit.** On looking at the error posted by Logcat this is what it has to say
11-20 09:10:31.931: I/Process(756): Sending signal. PID: 756 SIG: 9
11-20 09:39:57.001: D/dalvikvm(789): newInstance failed: p0 i0 [0 a1
11-20 09:39:57.001: D/AndroidRuntime(789): Shutting down VM
11-20 09:39:57.041: W/dalvikvm(789): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-20 09:39:57.091: E/AndroidRuntime(789): FATAL EXCEPTION: main
11-20 09:39:57.091: E/AndroidRuntime(789): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jkuat.project/com.jkuat.project.MapActivity}: java.lang.InstantiationException: com.jkuat.project.MapActivity
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.os.Looper.loop(Looper.java:123)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-20 09:39:57.091: E/AndroidRuntime(789): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 09:39:57.091: E/AndroidRuntime(789): at java.lang.reflect.Method.invoke(Method.java:521)
11-20 09:39:57.091: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-20 09:39:57.091: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-20 09:39:57.091: E/AndroidRuntime(789): at dalvik.system.NativeStart.main(Native Method)
11-20 09:39:57.091: E/AndroidRuntime(789): Caused by: java.lang.InstantiationException: com.jkuat.project.MapActivity
11-20 09:39:57.091: E/AndroidRuntime(789): at java.lang.Class.newInstanceImpl(Native Method)
11-20 09:39:57.091: E/AndroidRuntime(789): at java.lang.Class.newInstance(Class.java:1429)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-20 09:39:57.091: E/AndroidRuntime(789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-20 09:39:57.091: E/AndroidRuntime(789): ... 11 more
this is happening because you declared your MapActivity class as Abstract. Since it’s abstract, the VM cannot instantiate it when you launch that activity. Remove the abstract keyword and this should be resolved.