我正在尝试从savedInstanceState
. 我在这里将每个Bundle
单独添加:(节奏是对象数组)
@Override
public void onSaveInstanceState(Bundle outState){
outState.putInt("numParts",rhythm.length);
for(int index = 0;index<rhythm.length;++index){
outState.putSerializable(""+index,rhythm[index].beat);
}
super.onSaveInstanceState(outState);
}
当onRestoreInstanceState()
调用该方法时,我尝试在这里为我的rhythm
数组分配来自实例状态的对象:(它不为空)
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
rhythm = new Part[savedInstanceState.getInt("numParts")];
for(int index = 0; index<rhythm.length;++index){
Object middleMan =savedInstanceState.getSerializable(""+index);
if(middleMan==null){
System.out.println("It's null...");
}
rhythm[index]=(Part) middleMan;
}
}
当我每次ClassCastException
解析为 a 时,它都会抛出 a 。Part
部分实现Serializable
。为什么不允许我解析?我需要进行自定义序列化吗?请帮忙!