使用自定义对象捆绑HashMap

时间:2014-02-19 04:28:59

标签: java android hashmap bundle

我在HashMap onSaveInstanceState()中的SupportMapFragment捆绑onSaveInstanceState()时遇到问题。

logcat消息如下:

  

E / AndroidRuntime(12445):android.os.BadParcelableException:解组时的ClassNotFoundException:com.myapp.model.StationHashMap

已经尝试了一些解决方案:

  1. bundle.setClassLoader(getClass().getClassLoader());中,添加onSaveInstanceState()
  2. bundle.putSerializable(SAVE_INSTANCE_KEY_SATION_MAP, mStationMap);中,使用SupportMapFragment直接将 HashMap 放入捆绑包中,但logcat消息会更改为 com.myapp.model.Station
  3. 以下代码:

    • MapFragment.java :扩展onSaveInstanceState(),我想在Parcelable中保存以前的状态数据
    • StationHashMap.java :存储HashMap,实现Parcelable
    • Station.java :存储所有电台数据,同时实施 private HashMap<String, Station> mStationMap; ... @Override public void onSaveInstanceState(Bundle outState) { StationHashMap stationHashMap = new StationHashMap(); stationHashMap.setStationMap(mStationMap); outState.putParcelable(SAVE_INSTANCE_KEY_SATION_MAP, stationHashMap); }

    MapFragment.java

    public class StationHashMap implements Parcelable {
        private HashMap<String, Station> mStationMap = new HashMap<String, Station>();
    
        public StationHashMap() {
        }
    
        public void setStationMap(HashMap<String, Station> stationMap) {
            mStationMap = stationMap;
        }
    
        public HashMap<String, Station> getStationMap() {
            return mStationMap;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            Log.d(this.toString(), "in writeToParcel()");
    
            final int mapSize = mStationMap.size();
    
            dest.writeInt(mapSize);
    
            if (mapSize > 0) {
                for (Map.Entry<String, Station> entry : mStationMap.entrySet()) {
                    dest.writeString(entry.getKey());
    
                    Station station = entry.getValue();
    
                    dest.writeInt(station.getId());
                    dest.writeInt(station.getNo());
                    dest.writeString(station.getName());
                    dest.writeString(station.getAddress());
                    dest.writeDouble(station.getLat());
                    dest.writeDouble(station.getLng());
                    dest.writeString(station.getDesc());
                    dest.writeInt(station.getBikeNum());
                    dest.writeInt(station.getEmptyNum());
                }
            }
    
        }
    
        public static final Creator<StationHashMap> CREATOR = new Creator<StationHashMap>() {
    
            @Override
            public StationHashMap createFromParcel(Parcel source) {
                return new StationHashMap(source);
            }
    
            @Override
            public StationHashMap[] newArray(int size) {
                return new StationHashMap[size];
            }
    
        };
    
        private StationHashMap(Parcel source) {
            Log.d(this.toString(), "in StationHashMap()");
    
            final int mapSize = source.readInt();
    
            for (int i = 0; i < mapSize; i++) {
                String key = source.readString();
                Station station = new Station();
    
                station.setId(source.readInt());
                station.setNo(source.readInt());
                station.setName(source.readString());
                station.setAddress(source.readString());
                station.setLat(source.readDouble());
                station.setLng(source.readDouble());
                station.setDesc(source.readString());
                station.setBikeNum(source.readInt());
                station.setEmptyNum(source.readInt());
    
                mStationMap.put(key, station);
            }
        }
    }
    

    StationHashMap.java 中的方式来自:[问题]:Android:Passing a hash map between Activities

    StationHashMap.java

    public class Station implements Parcelable {
        private int mId;
        private int mNo;
        private String mName;
    
        public Station() {
    
        }
    
      // getters and setters...
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        private void readFromParcel(Parcel in) {
            mId = in.readInt();
            mNo = in.readInt();
            mName = in.readString();
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(mId);
            dest.writeInt(mNo);
        }
    
        private Station(Parcel in) {
            readFromParcel(in);
        }
    
        public static final Parcelable.Creator<Station> CREATOR = new Parcelable.Creator<Station>() {
    
            @Override
            public Station createFromParcel(Parcel source) {
                return new Station(source);
            }
    
            @Override
            public Station[] newArray(int size) {
                return new Station[size];
            }
        };
    
    }
    

    Station.java

    {{1}}

0 个答案:

没有答案