在运行时加载自定义ItemizedOverlay NullPointerException - Android

时间:2013-01-29 18:12:44

标签: java android google-maps overlay itemizedoverlay

我正在为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)

任何指针都将不胜感激!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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();