1

我正在为 android 制作一个地图应用程序,它允许用户放置自己的兴趣点。我想为放置的每种类型的点设置自定义图钉图标。当我尝试在运行时获取我的自定义 ItemizedOverlay 时,我得到了 NullPointerException。

在地图上添加一个点:

public void addPointToMap(PointOfInterest pointOfInterest) {

    OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <--- NullPointer here
    //Add item to collection in OurItemized Overlay
    itemizedOverlay.addOverlay(pointOfInterest);
    mapOverlays.clear();
    mapOverlays.add(myLocationOverlay);
    mapOverlays.add(itemizedOverlay);
    mapView.invalidate();
}

获取覆盖:

private OurItemizedOverlay getOverlayForPointOfInterest(PointOfInterest pointOfInterest) {

       String type = pointOfInterest.getType().toLowerCase();

       if(type.equals("monument")) {
           return monumentOverlay;
       }
       else if(type.equals("building")) {
           return buildingOverlay;
       }
       else if(type.equals("atm")) {
           return atmOverlay;
       }
       else if(type.equals("attraction")) {
           return attractionOverlay;
       }
       else if(type.equals("pub")) {
           return pubOverlay;
       }
       else if(type.equals("restaurant")) {
           return restaurantOverlay;
       }
       else if(type.equals("shop")) {
           return shopOverlay;
       }
       else if(type.equals("bridge")) {
           return bridgeOverlay;
       }
       else if(type.equals("station")) {
           return stationOverlay;
       }
       else if(type.equals("cafe")) {
           return cafeOverlay;
       }
       else if(type.equals("hotel")) {
           return hotelOverlay;
       }
       else {
           return defaultOverlay; 
       }
}

设置 Drawables 和 Overlays:

/**
 * Set the overlay objects up
 */
private void setItemizedOverlays() {

    //SET OVERLAY Items
    monumentOverlay = new OurItemizedOverlay(monumentIcon, this);
    buildingOverlay = new OurItemizedOverlay(buildingIcon, this);
    shopOverlay = new OurItemizedOverlay(shopIcon, this);
    attractionOverlay = new OurItemizedOverlay(attractionIcon, this);
    bridgeOverlay = new OurItemizedOverlay(bridgeIcon, this);
    stationOverlay = new OurItemizedOverlay(stationIcon, this);
    pubOverlay = new OurItemizedOverlay(pubIcon, this);
    restaurantOverlay = new OurItemizedOverlay(restaurantIcon, this);
    cafeOverlay = new OurItemizedOverlay(cafeIcon, this);
    atmOverlay = new OurItemizedOverlay(atmIcon, this);
    hotelOverlay = new OurItemizedOverlay(hotelIcon, this);


}

/**
 * Set the icons for each Drawable object
 */
private void setDrawableIcons() {

    defaultIcon = this.getResources().getDrawable(R.drawable.downarrow);
    monumentIcon = this.getResources().getDrawable(R.drawable.monument);
    shopIcon = this.getResources().getDrawable(R.drawable.shop);
    attractionIcon = this.getResources().getDrawable(R.drawable.attraction);
    buildingIcon = this.getResources().getDrawable(R.drawable.building);
    bridgeIcon = this.getResources().getDrawable(R.drawable.bridge);
    stationIcon = this.getResources().getDrawable(R.drawable.station);
    pubIcon = this.getResources().getDrawable(R.drawable.pub);
    restaurantIcon = this.getResources().getDrawable(R.drawable.restaurant);
    cafeIcon = this.getResources().getDrawable(R.drawable.cafe);
    atmIcon = this.getResources().getDrawable(R.drawable.atm);
    hotelIcon = this.getResources().getDrawable(R.drawable.hotel);


}

堆栈跟踪:

01-29 18:17:11.875: E/AndroidRuntime(12380): FATAL EXCEPTION: main
01-29 18:17:11.875: E/AndroidRuntime(12380): java.lang.NullPointerException
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.MainActivity.addPointToMap(MainActivity.java:140)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.MainActivity.onPointsOfInterestDownloaded(MainActivity.java:227)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.DownloadPointsOfInterest.onPostExecute(DownloadPointsOfInterest.java:67)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.DownloadPointsOfInterest.onPostExecute(DownloadPointsOfInterest.java:1)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.AsyncTask.finish(AsyncTask.java:602)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.Looper.loop(Looper.java:137)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.app.ActivityThread.main(ActivityThread.java:4441)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at java.lang.reflect.Method.invoke(Method.java:511)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at dalvik.system.NativeStart.main(Native Method)

任何指针将不胜感激!

4

2 回答 2

1

最有可能的错误 - 您进入了默认情况,我没有在 setItemizedOverlays 中看到 defaultOverlay 的初始化。但这对于调试器来说是一个很好的例子,在 getOverlayForPointOfInterest 中找出它进入了哪个分支,然后看看它为什么会为空。

于 2013-01-29T18:21:20.927 回答
0
OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <--- NullPointer here
    //Add item to collection in OurItemized Overlay
    itemizedOverlay.addOverlay(pointOfInterest);// here you overlay draw its k..
    mapOverlays.clear();  //now you clear mapoverlay so all overlay clear 
    mapOverlays.add(myLocationOverlay);//again you add maplocation overaly 
    mapOverlays.add(itemizedOverlay);
    mapView.invalidate();

用这个替换代码

 mapOverlays.clear(); 
OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <-- 
        //Add item to collection in OurItemized Overlay
        itemizedOverlay.addOverlay(pointOfInterest);
        // mapOverlays.add(myLocationOverlay); 
        mapOverlays.add(itemizedOverlay);
        mapView.invalidate(); 
于 2013-01-29T18:36:40.947 回答