Java 反序列化捆绑包并强制转换回实际类时出错 //这是下面提到的方法(在活动1中) 公共ArrayList getAllMatchEntries() { ArrayList结果=新建ArrayList(); 对于(int i=0;i0) { ... } }
说真的,我看不出我做错了什么 编辑: 我一直在使用的调试器在反序列化时显示了如下结构:Java 反序列化捆绑包并强制转换回实际类时出错 //这是下面提到的方法(在活动1中) 公共ArrayList getAllMatchEntries() { ArrayList结果=新建ArrayList(); 对于(int i=0;i0) { ... } },java,android,serialization,bundle,deserialization,Java,Android,Serialization,Bundle,Deserialization,说真的,我看不出我做错了什么 编辑: 我一直在使用的调试器在反序列化时显示了如下结构: // this is the method referred to below (in activity 1) public ArrayList<MatchEntry[]> getAllMatchEntries() { ArrayList<MatchEntry[]> result = new ArrayList<MatchEntry[]>();
// this is the method referred to below (in activity 1)
public ArrayList<MatchEntry[]> getAllMatchEntries()
{
ArrayList<MatchEntry[]> result = new ArrayList<MatchEntry[]>();
for(int i = 0; i < this.size(); i++)
{
result.add(this.get(i).getMatchInfo());
}
return result;
}
/**
* stores information about a single match
*/
public class MatchEntry implements Serializable
{...}
// Activity 1 - bundling
ArrayList<MatchEntry[]> allMatchEntries = _hadiths.getAllMatchEntries();
b.putSerializable("allMatchEntries", allMatchEntries);
Intent i = new Intent(...);
i.putExtras(b);
// Activity 2 - de-bundling
private ArrayList<MatchEntry[]> _allMatchEntries;
_allMatchEntries = (ArrayList<MatchEntry[]>) bundle.getSerializable("allMatchEntries");
// in fragment adapter, this is where the error occurs
class MyFragmentAdapter extends FragmentPagerAdapter
{
...
@Override
public Fragment getItem(int position)
{
// java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.a.b.MatchEntry[]
MatchEntry[] entries = (_allMatchEntries != null) ? _allMatchEntries.get(position) : null;
}
}
// this is the fragment
public class MyFragment extends Fragment
{
public static HadithFragment newInstance(int id, MatchEntry[] matchEntries)
{
MyFragment fm = new MyFragment();
Bundle args = new Bundle();
args.putInt("_id", id);
args.putSerializable("matchEntries", matchEntries); // store for onCreateView
fm.setArguments(args);
return fm;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// handle highlighting
MatchEntry[] _matchEntries = (MatchEntry[]) getArguments().getSerializable("matchEntries");
if(_matchEntries != null && _matchEntries.length > 0)
{
...
}
}
ArrayList
对象[1]
对象[1]
对象[2]
对象[1]
对象[2]
配对条目
配对条目
这是令人困惑的,因为我知道我序列化了一个
ArrayList
,正如您在我的代码中所看到的,那么为什么它在反序列化时有一个ArrayList
?基本上这就是我整个问题的要点。好吧,问题是您不能将Object[]array强制转换为任何特定的数组。这是Java特有的属性
您必须在循环中强制转换每个元素,或者可以通过以下方式缩短它:
ArrayList<Object[]>
Object[1]
Object[1]
Object[2]
Object[1]
Object[2]
MatchEntry
MatchEntry
不知怎的,类型丢失了,我不知道它的原因,但它有点奇怪。正如我所说,它将与ArrayList或者,您可以使用ArrayList
MatchEntry
是可序列化的,并且在
Object[] temp = _allMatchEntries.get(position)
MatchEntry[] target = new MatchEntry[temp.length]();
System.arraycopy(temp, 0, target, 0, a.length);
调试器显示,allMatchEntries
确实是一个ArrayList
对吗
如果是这样,那么意图包含一个正确的序列化,问题是反序列化,可能错误只是它不能强制转换数组,但它可以逐项执行,您尝试过吗
b.putSerializable("allMatchEntries", allMatchEntries);
@覆盖
公共片段getItem(int位置)
{
Object[]temp=(\u allMatchEntries!=null)?\u allMatchEntries.get(position):null;
MatchEntry[]entries=(\u allMatchEntries!=null)?\u allMatchEntries.get(位置):null;
对于(int i=0;iIt看起来您在某个时候向序列化的ArrayList
中添加了一个对象
数组。您选择较慢的可序列化
而不是较快的可打包
,这有什么原因吗?@ChrisHorner很好,直到我实现了整个功能,我不想优化速度。为什么不呢使用调试器并检查要强制转换到MatchEntry[]的内容?@kupsef我一直在使用调试器,请查看我的编辑我认为调试器显示的内容值得一提。我将尝试使用ArrayList的替代方案,看看它是否有效,是的,这很烦人。使用ArrayList
的替代方案有效,这就是我标记为答案的内容。
@Override
public Fragment getItem(int position)
{
Object[] temp = (_allMatchEntries != null) ? _allMatchEntries.get(position) : null;
MatchEntry[] entries = (_allMatchEntries != null) ? _allMatchEntries.get(position) : null;
for (int i=0; i<temp.length; i++){
entries[i]=(MatchEntry) temp[i];
}
//..
}