// 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<Object[]>
Object[1]
Object[1]
Object[2]
Object[1]
Object[2]
MatchEntry
MatchEntry
這是令人困惑,因爲我知道我連載您可以在我的代碼中看到ArrayList<MatchEntry[]>
,那麼爲什麼在反序列化中有ArrayList<Object[]>
?基本上這是我整個問題的要點。
看起來你已經在某個點向序列化的ArrayList添加了一個Object數組。 – corsair992
是否有一個原因,你選擇了較慢的'Serializable'而不是較快的'Parcelable'? –
@ChrisHorner,直到我得到整個功能的工作,我不想優化速度。 – sprocket12