[英]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. 我试图将自定义对象的集合传递给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. 我在代码中标记的行上获得了异常,并且相应的catch块将值获取为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
. 为了获取消息,我在遇到异常的那一行进行了
force step into
,并将其带到ClassCastException
类,在该类中我以调试模式看到此消息: 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
同样将对象作为intent.putExtra(PARAM_SENSOR_DATA,collection)传递会导致编译错误,并带有
cannot resolve method
消息
SensorReading
is an interface as shown below: SensorReading
是一个界面,如下所示:
public interface SensorReading<X, Y> {
String getSensorIdentifier();
List<Pair<X, Y>> getReadings();
void addReading(Pair<X, Y> reading);
}
and SensorReadings is the implementation: 和SensorReadings是实现:
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. 抛出ClassCastException,以指示代码已尝试将对象强制转换为不是实例的子类。 You are trying to cast Collection to Serializable but Collection does not implement Serializable per se.
您试图将Collection强制转换为Serializable,但是Collection本身未实现Serializable。
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 如果DataService(这是您要通过Intent启动的服务)是一个内部类,则可能要将集合存储在父类的临时属性中,并在服务中使用检索它,例如
ParentClass.this.collection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.