1

我正在开发一个显示带有多个标记的地图的 Android 应用程序。对于相同的代码,当我使用新应用程序运行时它可以正常工作,但是当我将它与我的应用程序集成时它就不行了。它给了我一个异常/错误,如下所示:

 07-24 14:19:55.737: ERROR/AndroidRuntime(1645): FATAL EXCEPTION: main
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maps/com.maps.DisplayMap}: java.lang.NullPointerException
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.os.Looper.loop(Looper.java:123)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at dalvik.system.NativeStart.main(Native Method)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645): Caused by: java.lang.NullPointerException
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at com.maps.DisplayMap.onCreate(DisplayMap.java:32)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    07-24 14:19:55.737: ERROR/AndroidRuntime(1645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我的代码:

package com.maps;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class DisplayMap extends MapActivity {
  private MapView map=null;
  private MyLocationOverlay me=null;
  private SitesOverlay sites=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    map=(MapView)findViewById(R.id.map);

    map.getController().setCenter(getPoint(19.159646,
                                            72.995664));
    map.getController().setZoom(17);
    map.setBuiltInZoomControls(true);

    sites=new SitesOverlay();
    map.getOverlays().add(sites);

    me=new MyLocationOverlay(this, map);
    map.getOverlays().add(me);
  }

  @Override
  public void onResume() {
    super.onResume();

    me.enableCompass();
  }  

  @Override
  public void onPause() {
    super.onPause();

    me.disableCompass();
  }  

  @Override
  protected boolean isRouteDisplayed() {
    return(false);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_S) {
      map.setSatellite(!map.isSatellite());
      return(true);
    }
    else if (keyCode == KeyEvent.KEYCODE_Z) {
      map.displayZoomControls(true);
      return(true);
    }
    else if (keyCode == KeyEvent.KEYCODE_H) {
      sites.toggleHeart();

      return(true);
    }

    return(super.onKeyDown(keyCode, event));
  }

  private GeoPoint getPoint(double lat, double lon) {
    return(new GeoPoint((int)(lat*1000000.0),
                          (int)(lon*1000000.0)));
  }

  private class SitesOverlay extends ItemizedOverlay<CustomItem> {
    private Drawable heart=null;
    private List<CustomItem> items=new ArrayList<CustomItem>();
    private PopupPanel panel=new PopupPanel(R.layout.popup);

    public SitesOverlay() {
      super(null);

      heart=getMarker(R.drawable.heart_full);

      items.add(new CustomItem(getPoint(40.748963847316034,
                                          -73.96807193756104),
                                "","",
                                getMarker(R.drawable.blue_full_marker),
                                heart));
      items.add(new CustomItem(getPoint(40.76866299974387,
                                          -73.98268461227417),
                                "",
                                "",
                                getMarker(R.drawable.orange_full_marker),
                                heart));
      items.add(new CustomItem(getPoint(40.765136435316755,
                                          -73.97989511489868),
                                "",
                                "",
                                getMarker(R.drawable.green_full_marker),
                                heart));
      items.add(new CustomItem(getPoint(40.70686417491799,
                                          -74.01572942733765),
                                "",
                                "",
                                getMarker(R.drawable.purple_full_marker),
                                heart));
      populate();
    }

    @Override
    protected CustomItem createItem(int i) {
      return(items.get(i));
    }

    @Override
    public void draw(Canvas canvas, MapView mapView,
                      boolean shadow) {
      super.draw(canvas, mapView, shadow);

    }

    @Override
    protected boolean onTap(int i) {
      OverlayItem item=getItem(i);
      GeoPoint geo=item.getPoint();
      Point pt=map.getProjection().toPixels(geo, null);

      View view=panel.getView();

      ((TextView)view.findViewById(R.id.latitude))
        .setText(String.valueOf(geo.getLatitudeE6()/1000000.0));
      ((TextView)view.findViewById(R.id.longitude))
        .setText(String.valueOf(geo.getLongitudeE6()/1000000.0));
      ((TextView)view.findViewById(R.id.x))
                              .setText(String.valueOf(pt.x));
      ((TextView)view.findViewById(R.id.y))
                              .setText(String.valueOf(pt.y));

      panel.show(pt.y*2>map.getHeight());

      return(true);
    }

    @Override
    public int size() {
      return(items.size());
    }

    void toggleHeart() {
      CustomItem focus=getFocus();

      if (focus!=null) {
        focus.toggleHeart();
      }

      map.invalidate();
    }

    private Drawable getMarker(int resource) {
      Drawable marker=getResources().getDrawable(resource);

      marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                        marker.getIntrinsicHeight());
      boundCenter(marker);

      return(marker);
    }
  }

  class PopupPanel {
    View popup;
    boolean isVisible=false;

    PopupPanel(int layout) {
      ViewGroup parent=(ViewGroup)map.getParent();

      popup=getLayoutInflater().inflate(layout, parent, false);

      popup.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          hide();
        }
      });
    }

    View getView() {
      return(popup);
    }

    void show(boolean alignTop) {
      RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
      );

      if (alignTop) {
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp.setMargins(0, 20, 0, 0);
      }
      else {
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.setMargins(0, 0, 0, 60);
      }

      hide();

      ((ViewGroup)map.getParent()).addView(popup, lp);
      isVisible=true;
    }

    void hide() {
      if (isVisible) {
        isVisible=false;
        ((ViewGroup)popup.getParent()).removeView(popup);
      }
    }
  }

  class CustomItem extends OverlayItem {
    Drawable marker=null;
    boolean isHeart=false;
    Drawable heart=null;

    CustomItem(GeoPoint pt, String name, String snippet,
               Drawable marker, Drawable heart) {
      super(pt, name, snippet);

      this.marker=marker;
      this.heart=heart;
    }

    @Override
    public Drawable getMarker(int stateBitset) {
      Drawable result=(isHeart ? heart : marker);

      setState(result, stateBitset);

      return(result);
    }

    void toggleHeart() {
      isHeart=!isHeart;
    }
  }
}
4

0 回答 0