Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将HashMap与自定义对象绑定_Java_Android_Hashmap_Bundle - Fatal编程技术网

Java 将HashMap与自定义对象绑定

Java 将HashMap与自定义对象绑定,java,android,hashmap,bundle,Java,Android,Hashmap,Bundle,我对onSaveInstanceState()中的BundleHashMap有问题 logcat消息如下所示: E/AndroidRuntime(12445):android.os.BadParcelableException:ClassNotFoundException解组时:com.myapp.model.StationHashMap 已经尝试了一些解决方案: 在onSaveInstanceState()中,添加bundle.setClassLoader(getClass().getClas

我对
onSaveInstanceState()
中的Bundle
HashMap
有问题

logcat消息如下所示:

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

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

  • onSaveInstanceState()
    中,添加
    bundle.setClassLoader(getClass().getClassLoader())
  • onSaveInstanceState()
    中,使用
    bundle.putSerializable(保存实例键映射,mStationMap)
    直接将HashMap放在bundle中,但是logcat消息会变成com.myapp.model.Station
  • 以下代码:

    • MapFragment.java:扩展
      SupportMapFragment
      ,我想在
      onSaveInstanceState()中保存以前的状态数据
    • StationHashMap.java:存储HashMap,实现
      Parcelable
    • Station.java:存储所有站点数据,还实现了
      Parcelable
    MapFragment.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);
      }
    
    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);
            }
        }
    }
    
    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];
            }
        };
    
    }
    
    私有HashMap mStationMap;
    ...
    @凌驾
    SaveInstanceState上的公共无效(束超出状态){
    StationHashMap StationHashMap=新的StationHashMap();
    setStationMap(mStationMap);
    outState.putParcelable(保存实例、密钥、状态图、stationHashMap);
    }
    
    StationHashMap.java中的方法是引用自:[问题]:

    StationHashMap.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);
      }
    
    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);
            }
        }
    }
    
    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];
            }
        };
    
    }
    
    公共类StationHashMap实现可包裹{
    私有HashMap mStationMap=新HashMap();
    公共站点hashmap(){
    }
    公共无效setStationMap(HashMap stationMap){
    mStationMap=stationMap;
    }
    公共HashMap getStationMap(){
    返回mStationMap;
    }
    @凌驾
    公共int描述内容(){
    返回0;
    }
    @凌驾
    公共无效写入包裹(包裹目的地,内部标志){
    Log.d(this.toString(),“in writeToParcel()”;
    final int-mapSize=mStationMap.size();
    目的写入(mapSize);
    如果(贴图大小>0){
    对于(Map.Entry:mStationMap.entrySet()){
    dest.writeString(entry.getKey());
    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());
    }
    }
    }
    公共静态最终创建者=新创建者(){
    @凌驾
    公共站点HashMap createFromParcel(地块源){
    返回新的StationHashMap(源);
    }
    @凌驾
    public StationHashMap[]新数组(整数大小){
    返回新的StationHashMap[大小];
    }
    };
    专用站点HashMap(地块源){
    Log.d(this.toString(),“在StationHashMap()中”);
    final int mapSize=source.readInt();
    对于(int i=0;i
    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);
      }
    
    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);
            }
        }
    }
    
    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];
            }
        };
    
    }
    
    公共类站实现可包裹{
    私人int mId;
    私人国际mNo;
    私有字符串mName;
    公共电台(){
    }
    //接球手和接球手。。。
    @凌驾
    公共int描述内容(){
    返回0;
    }
    private void readFromParcel(地块位于中){
    mId=in.readInt();
    mNo=in.readInt();
    mName=in.readString();
    }
    @凌驾
    公共无效写入包裹(包裹目的地,内部标志){
    目的地写入(mId);
    目的地记录(mNo);
    }
    私人车站(包裹内){
    从包裹中读取;
    }
    public static final Parcelable.Creator=新建Parcelable.Creator(){
    @凌驾
    公共站点createFromParcel(地块源){
    返回新站点(源);
    }
    @凌驾
    公共电台[]新阵列(整数大小){
    返回新站点[大小];
    }
    };
    }
    
    我现在也有同样的问题。看起来我还没有尝试的唯一选项是使用片段的参数保存状态。但是那真的是一个糟糕的练习。我用上面的方法练习。