问题描述
我需要将两个SparseArray存储在一个捆绑包中。
I need to store in a Bundle two SparseArray.
所以它是这样的:
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putSparseParcelableArray("guybrushInsulti", (SparseArray<String>) gameEngine.getDialogs().getInsultKnow());
state.putSparseParcelableArray("guybrushControInsulti",(SparseArray<String>) gameEngine.getDialogs().getControInsultKnow());
state.putSerializable("level", gameEngine.getWorld().getLevel());
}
Eclipse说:
The method putSparseParcelableArray(String, SparseArray<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, SparseArray<String>)
OnRestoreInstanceState:
OnRestoreInstanceState:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
SparseArray<String> mInsultiKnow = savedInstanceState.getSparseParcelableArray("guybrushInsulti");
SparseArray<String> mControInsultiKnow = savedInstanceState.getSparseParcelableArray("guybrushControInsulti");
//Ripristino insulti
gameEngine.getDialogs().loadControInsult(mLevel , true);
gameEngine.getDialogs().loadInsult(mLevel, true, mInsultiKnow, mControInsultiKnow);
}
错误:
Bound mismatch: The generic method getSparseParcelableArray(String) of type Bundle is not applicable for the arguments (String). The inferred type String is not a valid substitute for the bounded parameter <T extends Parcelable>
我的问题是什么? :(
What's my problem? :(
推荐答案
在您的情况下(其中SparseArray为字符串),我认为您不能使用方法(putSparseParcelableArray)。
In your case (where the SparseArray is of String), i think you cannot use the method (putSparseParcelableArray).
仅当稀疏数组的对象类型实现了Parcelable接口,而String类没有实现此方法时,才可以使用此方法。
This method can only be used when the Object type of the Sparse Array implements Parcelable interface, and String class doesn't implement this.
引用:
直到我们没有任何具体解决方案时,您可以这样做,
Till the time we don't get any concrete solution you can do this,While saving the SparseArray, put this data in the list and save that list the outState bundle.
然后在onRestoreInstanceState中,检索列表并将其再次转换为SparseArray。
And in onRestoreInstanceState, retrieve the list and convert it to the SparseArray again.
这篇关于带有SparseArray< String>的Android捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!