2014-03-28 50 views
4

我有一個自定義類的ArrayList從活動傳遞給另一個。我讓班級執行Parcelable。但是,每當我使用傳遞的ArrayList時,我都會得到NullPointException。 所以我決定檢查getParcelableArrayList()的返回值的類,並且我得到了java.lang.Integer。我想知道如何解釋這一點。getParcelableArrayList返回null

代碼的第一個活動的:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(this, WeiboBrowseActivity.class); 
    intent.putParcelableArrayListExtra(WeiboBrowseActivity.KEY_WEIBO_INFO_LIST, weiboInfoList); 
    intent.putExtra(WeiboBrowseActivity.KEY_SELECTED_WEIBO_INDEX, arg2); 
    startActivity(intent); 
    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left); 
} 

第二活動的代碼:

private void initWeiboInfos() { 
    Log.d("cosmo","the type is: " + getIntent().getExtras().get(KEY_WEIBO_INFO_LIST).getClass().getName()); 
    weiboInfos = getIntent().getExtras().getParcelableArrayList(KEY_WEIBO_INFO_LIST); 
    if (weiboInfos == null) { 
     Log.d("cosmo", "weiboInfos null");  //I got this log 
    } else { 
     Log.d("cosmo", "weiboInfos not null"); 
    } 
} 

代碼的Parcelable類:

public class WeiboInfo implements Parcelable { 
    private String absolutePath; 
    private long createTimestamp; 

    public WeiboInfo() { 
     super(); 
     this.setAbsolutePath(""); 
     this.setCreateTimestamp(new Date().getTime()); 
    } 

    public WeiboInfo(String absolutePath, long createTimestamp) { 
     super(); 
     this.setAbsolutePath(absolutePath); 
     this.setCreateTimestamp(createTimestamp); 
    } 

    public WeiboInfo(Parcel in) { 
     super(); 
     this.setAbsolutePath(in.readString()); 
     this.setCreateTimestamp(in.readLong()); 
    } 

    public String getAbsolutePath() { 
     return absolutePath; 
    } 

    public void setAbsolutePath(String absolutePath) { 
     this.absolutePath = absolutePath; 
    } 

    public long getCreateTimestamp() { 
     return createTimestamp; 
    } 

    public void setCreateTimestamp(long createTimestamp) { 
     this.createTimestamp = createTimestamp; 
    } 

    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     // TODO Auto-generated method stub 
     dest.writeString(absolutePath); 
     dest.writeLong(createTimestamp); 
    } 

    public void readFromParcel(Parcel in) { 
     absolutePath = in.readString(); 
     createTimestamp = in.readLong(); 
    } 

    public static final Parcelable.Creator<WeiboInfo> CREATOR = new Parcelable.Creator<WeiboInfo>() { 
     public WeiboInfo createFromParcel(Parcel in) { 
      return new WeiboInfo(in); 
     } 

     public WeiboInfo[] newArray(int size) { 
      return new WeiboInfo[size]; 
     } 
    }; 
} 

這裏有兩個關鍵:

public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST"; 
    public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_WEIBO_INFO_LIST"; 

回答

4
public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST"; 
public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_WEIBO_INFO_LIST"; 

它像一本字典。您不能有重複的密鑰。這就是爲什麼你越來越int,因爲這裏

intent.putParcelableArrayListExtra(WeiboBrowseActivity.KEY_WEIBO_INFO_LIST, weiboInfoList); 
intent.putExtra(WeiboBrowseActivity.KEY_SELECTED_WEIBO_INDEX, arg2); 

後者是壓倒前者。改變它在

public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST"; 
public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_SELECTED_WEIBO_INDEX"; 
+0

哦!我很愚蠢。我複製了KEY_WEIBO_INFO來定義KEY_SELECT_WEIBO_INDEX,但我忘記了改變它的值。謝謝回答! – cosmozhang

+0

不客氣 – Blackbelt