简体   繁体   中英

java.util.hashmap$values cannot be cast to java.io.serializable in android putExtra method

I am trying to pass a collection of custom objects to a service in android.

try {
    FileParser parser = new FileParserImpl();
    Collection<SensorReading<Long, Float>> collection = parser.parse(fileContents);
    Intent intent = new Intent(FileBrowserActivity.this, DataService.class);

    intent.putExtra(PARAM_SENSOR_DATA, (Serializable)collection);  //exception here.
    startService(intent);
catch (Exception e) {   //e = null, 
    Log.e(TAG, "We go an exception", e);
}

I get the exception at line marked in the code and the corresponding catch block gets the value as null. To get the message I do a force step into at the line where I get exception and it take me to the class ClassCastException where I see this message in debug mode : java.util.hashmap$values cannot be cast to java.io.serializable . I followed the suggestion given here

Also passing the object as intent.putExtra(PARAM_SENSOR_DATA, collection) gives compilation error with message that cannot resolve method

SensorReading is an interface as shown below:

public interface SensorReading<X, Y> {
    String getSensorIdentifier();
    List<Pair<X, Y>> getReadings();
    void addReading(Pair<X, Y> reading);
}

and SensorReadings is the implementation:

public class SensorReadings implements SensorReading<Long, Float>, Serializable {
    private static final long serialVersionUID = -2518143671167959230L;
    String location;
    String sensorName;

    public SensorReadings(String location, String sensorName) {
        //constructor
    }

    List<Pair<Long, Float>> timeStampReadingTuple;

    public void addReading(Pair<Long, Float> pair) {
        timeStampReadingTuple.add(pair);
    }

    public List<Pair<Long, Float>> getReadings() {
        return timeStampReadingTuple;
    }

    @Override
    public String getSensorIdentifier() {
        return sensorName + "@ " + location;
    }

    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        location = (String) in.readObject();
        sensorName = (String) in.readObject();
        int size = (int) in.readObject();
        timeStampReadingTuple = Lists.newArrayListWithCapacity(size);
        for(int i=0; i<size; ++i) {
            long timeStamp = (long) in.readObject();
            float reading = (float) in.readObject();
            timeStampReadingTuple.add(new Pair<Long, Float>(timeStamp, reading));
        }
    }

    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeObject(location);
        out.writeObject(sensorName);
        out.writeObject(timeStampReadingTuple.size());
        for(Pair<Long, Float> pair : timeStampReadingTuple) {
            out.writeObject(pair.first);
            out.writeObject(pair.second);
        }
    }
}

I think the problem is that...

The ClassCastException is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. You are trying to cast Collection to Serializable but Collection does not implement Serializable per se.

It the DataService (which is the service you want to start through your Intent) is an inner class you may want to store the collection in a temporary attribute of the parent class and use retrieve it within the service like

 ParentClass.this.collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM