Hashtable <string,object>返回null </string,object>

时间:2014-01-23 13:48:19

标签: java android google-maps

我创建了一个Hashtable,其中key(id)是一个字符串,值是一个对象,

public void addSpot(String id) {
        Spot spot = new Spot(id);
        SpotMap.addSpot(id, spot); // adds spot to table
        spot.getMarkerOptions().position(new LatLng(28, 30));
        spot.addMark(map); // adds mark to map
            Log.i("SPOT_TABLE", SpotMap.getSize());
    }

    public void removeSpot(String id) {
        try{
        Spot spot=SpotMap.getSpotMap().get(id);
        spot.removeMark(spot.getMarker());
        SpotMap.removeSpot(id);
            Log.i("SPOT_TABLE", SpotMap.getSize());}
        catch(Exception e){}

    }

这是SpotMap类

package com.example.myapp;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import android.util.Log;

import com.example.myapp.Spot;

public class SpotMap {

    private static Hashtable<String, Spot> spotMap = new Hashtable();

    public static Hashtable<String, Spot> getSpotMap() {
        return spotMap;
    }

    // public static void setMarkermap(Hashtable<String, Spot> markermap) {
    // MarkerMap.markerMap = markermap;
    // }

    public static void addSpot(String id, Spot spot) {
        spotMap.put(id, spot);

    }

    public static void removeSpot(String id) {
        spotMap.remove(id); // removes spot from table
    }

    public static void printTable(){
        Enumeration key = spotMap.keys();
        while (key.hasMoreElements()) {
            System.out.println(spotMap.get(key.nextElement())); 

        }
    }
}

Spot类

package com.example.myapp;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Marker;

    public class Spot {
        private String id;
        private MarkerOptions markerOptions;
        private Marker marker;

        public Spot(String id) {
            this.id = id;
            markerOptions = new MarkerOptions();
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setMarkerOptions(MarkerOptions markerOptions) {
            this.markerOptions = markerOptions;
        }

        public void addMark(GoogleMap map) {
            marker = map.addMarker(markerOptions);
        }

        public void removeMark(Marker marker) {
            marker.remove();

        }

        public MarkerOptions getMarkerOptions() {
            return markerOptions;
        }

        public void setMarker(Marker marker) {
            this.marker = marker;
        }

        public Marker getMarker() {
            return marker;
        }
        /*
         * public Spot getSpot() { return this; }
         */
        public String toString(){
            return id;

        }
    }

当我添加1个点并移除它时应用程序运行良好,但是当我添加2个连续点(具有相同的id)并调用remove函数时,我得到NullPointException并且它不会删除标记 LogCat

01-23 10:10:36.698: I/SPOT_TABLE(5209): 1
01-23 10:10:38.518: I/SPOT_TABLE(5209): 0
01-23 10:10:40.098: I/SPOT_TABLE(5209): 1
01-23 10:10:40.718: I/SPOT_TABLE(5209): 1
01-23 10:10:41.748: I/SPOT_TABLE(5209): 1
01-23 10:10:42.638: I/SPOT_TABLE(5209): 0
01-23 10:10:44.488: I/SPOT_TABLE(5209): 1
01-23 10:10:45.048: I/SPOT_TABLE(5209): 0
01-23 10:10:48.628: I/SPOT_TABLE(5209): 1
01-23 10:10:48.938: I/SPOT_TABLE(5209): 1
01-23 10:10:49.368: I/SPOT_TABLE(5209): 1
01-23 10:10:49.448: I/SPOT_TABLE(5209): 1
01-23 10:10:49.708: I/SPOT_TABLE(5209): 1
01-23 10:10:50.678: I/SPOT_TABLE(5209): 1
01-23 10:10:50.938: I/SPOT_TABLE(5209): 0

LogCat会打印Hashtable的大小,正如您所看到的,添加相同的键会像往常一样替换旧的键,也会删除按钮似乎有效...但现在我认为这是一个标记问题

2 个答案:

答案 0 :(得分:0)

我的猜测是,当您尝试第二次添加addMarker()时,null方法会返回Marker

这会导致您的marker实例变量变为null,并在NullPointerException方法中为您提供removeSpot()

答案 1 :(得分:0)

它现在正在工作,我必须编辑addSpot函数,在插入新的entery之前删除旧的entery 但直到现在,我不知道为什么

spot.removeMark(spot.getMarker());

无法正常工作

  public void addSpot(String id) {
            Spot spot = new Spot(id);
            if (SpotMap.getSpotMap().containsKey(id)) {
                removeSpot(id);
            }
            spot.getMarkerOptions().position(new LatLng(28, 30));
            SpotMap.addSpot(id, spot); // adds spot to table
            spot.addMark(map); // adds mark to map
            Log.i("SPOT_TABLE", SpotMap.getSize());
        }