2012-06-26 38 views
-1

可能重複:
Can't pass an ArrayList<Parcelable> to an activity空接收Parcelable

昨天我得到這個工作,我取得了一些代碼改組和現在的東西不工作了。看了一會兒,我找不到問題。

我傳遞Parcelables並的一個ArrayList的活動:

ArrayList<MyParcelable> myArrayListOfParcelables = new ArrayList<MyParcelable>(); 

//... 

Intent intent = new Intent(this, ChooseGiftDetailsActivity.class); 
intent.putParcelableArrayListExtra(Constants.FOO, myArrayListOfParcelables); 
startActivity(intent); 

接收機:

ArrayList<Parcelable> parcelableList = (ArrayList<Parcelable>)getIntent().getParcelableArrayListExtra(Constants.FOO); 

parcelableList總是空雖然我送一個。

MyParcelable當然擴展Parcelable。我已經使用putExtra測試MyParcelable路過一個對象,它始終工作:

Parcelable p = (Parcelable)getIntent().getExtras().getParcelable(Constants.BAR); 

MyParcelable p = (MyParcelable)getIntent().getExtras().getParcelable(Constants.BAR); 

我把一些調試消息在MyParcelable的代碼,我看到路過的名單隻有書面方式方法時,執行,但它永遠不會開始閱讀,甚至不執行createFromParcel。這是什麼?

+0

遵循這一嘖嘖http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html – Akram

+0

我這樣做,但它不工作 – Ixx

+0

他們,你必須失去了一些東西 – Akram

回答

0

我假設你有一個CREATOR,如果不是請創建一個。然後,我會嘗試這樣的:

ArrayList<MyParcelable> myArrayListOfParcelables = new ArrayList<MyParcelable>(); 

//... 

Intent intent = new Intent(this, ChooseGiftDetailsActivity.class); 
intent.putIntExtra(Constants.FOO_COUNT, myArrayListOfParcelables.size()); 
MyParcelable p; 
for (int i = 0; i < myArrayListOfParcelables.size(); i++){ 
    p = myArrayListOfParcelables.get(i); 
    intent.putParcelableExtra(Constants.FOO + "_" + i, p); 
} 
startActivity(intent); 

然後再讀一遍這樣的:

Intent intent = getIntent(); 
int cnt = intent.getIntExtra(Constants.FOO_COUNT); 
ArrayList<MyParcelable> myArrayListOfParcelables = new ArrayList<MyParcelable>(cnt); 
MyParcelable p; 
for (int i = 0; i < cnt; i++){ 
    p = (MyParcelable)intent.getParcelableExtra(Constants.FOO + "_" + i); 
    myArrayListOfParcelables.add(p); 
} 

這種方式,你有abillity看到什麼引擎蓋下會更好的。可能是您的Parcelables數組太大而無法獲取Parceled。我試過一次傳遞一個位圖並且有類似的問題。

+0

嗯......好吧,這將是我會嘗試的下一件事。 – Ixx