本文介绍了如何sparsearray存放在包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SparseArray< myObject的> ,并希望将其存储在包中的的onSaveInstanceState 方法,在我的活动,在的OnCreate 恢复。我在捆绑发现 putSparseParcelableArray 方法把SparseArray和的onSaveInstanceState 这样做的方法:

I have a SparseArray<myObject> and want to store it in bundle in onSaveInstanceState method in my activity and restore it in oncreate. I found putSparseParcelableArray method for put SparseArray in bundle and did this in onSaveInstanceState method:

bundle.putSparseParcelableArray("mySparseArray", mySparseArray);

但显示文摘此错误:

But eclips shows this error:

The method putSparseParcelableArray(String, SparseArray<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, SparseArray<myObject>)

和快速修复被铸造的说法 mySparsArray SparseArray&LT ;?扩展Parcelable&GT; ,但如果我这样做,并把它在onCreate方法:

And the quick fix is casting argument mySparsArray to SparseArray<? extends Parcelable>, but if I do so and get it in onCreate method:

mySparseArray = (SparseArray<myObject>) savedInstanceState.getSparseParcelableArray("mySparseArray");

它得到这个错误:

It gets this error:

Cannot cast from SparseArray<Parcelable> to SparseArray<myObject>

如果这样是错的,什么是在包放mySparseArray解决?任何帮助将是非常美联社preciated。

If this way is wrong, what is the solution for put mySparseArray in bundle?Any help would be much appreciated.

推荐答案

您的类实现 Parcelable ,应该有所谓的静态最终成员变量 CREATOR 类型 Parcelable.Creator&LT; myObject的&GT;

Your class should implement Parcelable and should have a static final member variable called CREATOR of type Parcelable.Creator<myObject>.

这篇关于如何sparsearray存放在包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 03:22