2014-01-09 21 views
2
// 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[]>?基本上這是我整個問題的要點。

+0

看起來你已經在某個點向序列化的ArrayList添加了一個Object數組。 – corsair992

+0

是否有一個原因,你選擇了較慢的'Serializable'而不是較快的'Parcelable'? –

+0

@ChrisHorner,直到我得到整個功能的工作,我不想優化速度。 – sprocket12

回答

1

那麼,問題是,你不能將Object []數組投射到任何特定的數組。這是一個Java特定的屬性。

你必須每個元素投在一個循環中,也可以縮短這樣說:

Object[] temp = _allMatchEntries.get(position) 
MatchEntry[] target = new MatchEntry[temp.length](); 
System.arraycopy(temp, 0, target, 0, a.length); 

不知何故類型丟失了,我不知道它的原因,但它有點奇怪。正如我所說的,它可以與ArrayList < String []>一起工作,也許是因爲String類是一個常見的類,並且序列化器專門處理它。有人可以在這一點上說一些,它真的讓我感到困惑......

或者你可以使用ArrayList < ArrayList < MatchEntry >>。這將起作用,至少我已經測試過它。

+0

我會嘗試使用ArrayList >替代它,看它是否有效,是的,這很煩人。 – sprocket12

+0

使用'ArrayList >'的替代方法有效,這就是我標記爲我的答案。 – sprocket12

0

所以,我想我們相信,MatchEntry是序列化和後

b.putSerializable("allMatchEntries", allMatchEntries); 

調試器顯示確實allMatchEntriesArrayList<MatchEntry[]>吧?

如果是這樣,意圖包含一個正確的序列化,並且問題是反序列化,可能錯誤只是它不能投射數組,但它可以逐項執行,你試過嗎?

@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]; 
     } 
     //.. 
}