2011-11-20 24 views
1

我正在編寫一個應用程序,它從web服務中檢索座標,然後將它們與我的當前位置一起繪製在地圖上:我能夠編寫代碼,但沒有任何錯誤我在模擬器上運行它崩潰....可能是什麼問題:這是代碼:繪製Android中的Web服務座標的錯誤

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; 
    } 

} 
} 

編輯。 * *在看着張貼logcat的錯誤,這是它有什麼要說

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 
+0

如果您使用的是Eclipse,請在window-> show view-> other-> android-> LogCat下打開LogCat窗口。它可以打印出仿真器/設備正在發生的所有事情。再次運行你的程序,當它崩潰滾動並尋找一個巨大的紅色文本牆。這通常是你的錯誤發生的地方。這應該讓你更深入地瞭解你的問題。 – vince88

+0

我已經這樣做了:這是什麼日誌貓不得不說: –

回答

0

這種情況正在發生,因爲你宣佈你的MapActivity類爲抽象。由於它是抽象的,當啓動該活動時,VM不能實例化它。刪除抽象關鍵字,這應該解決。

+0

謝謝克里斯...它總是工作 –