使用多个叠加时,NullPointerException onTouchEvent

时间:2012-04-18 20:20:13

标签: android google-maps

我正在研究一个试图同时在mapView上显示多个不同叠加层的应用程序。不同叠加的原因需要是不同的颜色。我有一个允许滚动和缩放的工作地图,但是在添加多个叠加后,每当我尝试滚动时,地图现在都会给出一个空指针异常,并且缩放按钮已经消失。

这是我的地图活动:

package com.example.UVAMapsProject;

import java.util.ArrayList;
import java.util.List;

import com.example.UVAMapsProject.R;
import com.example.UVAMapsProject.R.drawable;
import com.example.UVAMapsProject.R.id;
import com.example.UVAMapsProject.R.layout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class MapsActivity extends MapActivity {
    /** Called when the activity is first created. */
    Context mContext;
    MapView mapView;
    private MapController mapController;
    List<Overlay> mapOverlays;
    Drawable drawableStandard;
    Drawable drawableComplete;
    Drawable drawableNext;
    HelloItemizedOverlay itemizedOverlayStandard;
    HelloItemizedOverlay itemizedOverlayNext;
    HelloItemizedOverlay itemizedOverlayComplete;

    ArrayList<GeoPoint> overlayPoints = new ArrayList<GeoPoint>();
    ArrayList<Destination> tourListTemp = SplashActivity.tourApp1.getTheTour().getTourList();

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

        //get current center, create map at center
        int lat = (int)NavigatorActivity.currentLocation.getLatitude();
        int lon = (int)NavigatorActivity.currentLocation.getLongitude();
        GeoPoint point = new GeoPoint((int)(lat*1e6), (int)(lon*1e6) );
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(false);
        mapController = mapView.getController();
        mapController.setZoom(10);
        mapController.setCenter(point);

        // Initializes three different images and itemized overlays for each type of overlay
        drawableStandard = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlayStandard = new HelloItemizedOverlay(drawableStandard, mapView);
        drawableNext = this.getResources().getDrawable(R.drawable.androidmarkernext);
        itemizedOverlayNext = new HelloItemizedOverlay(drawableNext, mapView);
        drawableComplete = this.getResources().getDrawable(R.drawable.androidmarkercomplete);
        itemizedOverlayComplete = new HelloItemizedOverlay(drawableStandard, mapView);

        // get points of Destinations & make overlays
        addOverlayPoints();

        mapController.zoomToSpan(itemizedOverlayStandard.getLatSpanE6(), itemizedOverlayStandard.getLonSpanE6());

        final MyLocationOverlay myPosition = new MyLocationOverlay(this,mapView);
        mapOverlays = mapView.getOverlays();  
        mapOverlays.add(myPosition);
        mapOverlays.add(itemizedOverlayStandard);
        mapOverlays.add(itemizedOverlayNext);
        mapOverlays.add(itemizedOverlayComplete);
        myPosition.enableMyLocation();

    }


    public void addOverlayPoints(){
        for (Destination d: tourListTemp) {
        GeoPoint temp = new GeoPoint((int)(tourListTemp.get(tourListTemp.indexOf(d)).getLatitude()*1e6),
                (int)(tourListTemp.get(tourListTemp.indexOf(d)).getLongitude()*1e6));
        overlayPoints.add(temp);
        OverlayItem overlayitem = new OverlayItem(temp, d.getName(), d.getInfo());
        itemizedOverlayStandard.addOverlay(overlayitem);
        }
        itemizedOverlayNext.addOverlay(itemizedOverlayStandard.removeOverlay(tourListTemp.get(0)));
    }

这是我的叠加课程:

package com.example.UVAMapsProject;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class HelloItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;
    MapView mapView;

    public HelloItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public HelloItemizedOverlay(Drawable defaultMarker, MapView mv) {
          super(boundCenterBottom(defaultMarker));
          mContext = mv.getContext();
    }

    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }

    @Override
    protected OverlayItem createItem(int i) {
      return mOverlays.get(i);
    }

    public int getOverlayIndex(Destination d){
        int out = SplashActivity.tourApp1.getTheTour().getTourList().indexOf(SplashActivity.tourApp1.getNextDestination());
        return out;
    }

    public OverlayItem removeOverlay(Destination d) {
        int index = getOverlayIndex(d);
        OverlayItem temp = mOverlays.get(index);
        mOverlays.remove(index);
        return temp;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

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

}

以下是点击地图时收到的错误:

04-18 15:56:46.980: E/AndroidRuntime(3262): FATAL EXCEPTION: main
04-18 15:56:46.980: E/AndroidRuntime(3262): java.lang.NullPointerException
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.google.android.maps.ItemizedOverlay.getItemsAtLocation(ItemizedOverlay.java:617)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.google.android.maps.ItemizedOverlay.getItemAtLocation(ItemizedOverlay.java:586)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.google.android.maps.ItemizedOverlay.handleMotionEvent(ItemizedOverlay.java:498)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.google.android.maps.ItemizedOverlay.onTouchEvent(ItemizedOverlay.java:572)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:643)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.view.View.dispatchTouchEvent(View.java:3766)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.os.Looper.loop(Looper.java:123)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at java.lang.reflect.Method.invokeNative(Native Method)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at java.lang.reflect.Method.invoke(Method.java:521)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-18 15:56:46.980: E/AndroidRuntime(3262):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

HelloItemizedOverlay构造函数中,您需要调用populate()