我有一個Android應用程序,實現了Parcelable接口的自定義對象。他們設置它的方式是,我的程序最初從包中的文件創建自定義類Products
的ArrayList
。我可以看到並確認arraylist和它的實例變量正確地填充。這個類有幾個實例變量,一個是ArrayList
,另一個是String
類。記住這個事實。Android:自定義Parcel類無需拋出異常崩潰應用程序
我試圖將ArrayList<Product>
進入一個新的活動,像這樣:通過使用
app_products = getIntent().getParcelableArrayListExtra("Products");
try {
Intent i = new Intent(RootActivity.this, ProductsActivity.class); //Intent from this activity to the next
i.putParcelableArrayListExtra("Products", app_products); //Puts my ArrayList<Class A> as an extra
startActivity(i); //Launch the activity
}
catch(Exception e){
Log.d("Activity Error", "Error Here:" + e.getMessage());
}
我從我的新活動的意圖通過拉動ArrayList
了收集信息反饋
對於我的自定義類,它看起來像這樣,以及實現的Parcelable方法。
public class Product implements Parcelable{
private String name;
private String cost;
private ArrayList<String> similarItems;
public Product{
name = null;
cost = null;
similarItems = new ArrayList<String>();
}
public Product(String name, String cost){
this();
this.name = name;
this.cost = cost;
}
public addSimilarItem(String item){
similarItems.add(item);
}
public static final Parcelable.Creator<Product> CREATOR
= new Parcelable.Creator<Product>()
{
public Product createFromParcel(Parcel in) {
return new Product(in);
}
public Product[] newArray(int size) {
return new Product[size];
}
};
public int describeContents(){
return 0;
}
private Product(Parcel in){
name = in.readString();
cost = in.readString();
similarItems = in.readArrayList(String.class.getClassLoader());
}
public void writeToParcel(Parcel out, int flags){
out.writeString(name);
out.writeString(cost);
out.writeList(similarItems);
}
}
所以這個工作得很好WITHOUT在班裏被添加我的字符串數組列表
註釋掉out.writeList(similarItems);
也similarItems = in.readArrayList(String.class.getClassLoader());
但一旦你回到它們添加到類,應用程序崩潰但它甚至沒有提供調試信息。我已經包裝了try-catch
聲明中的所有內容,而且android甚至不會在跳板上報告使用正常對話框崩潰的應用程序。我真的很茫然。
值得一提的是,儘管android不會拋出異常,但我已經使用了一些日誌語句來了解程序崩潰的位置。我可以看到,我的ArrayList中的所有項都經歷了writeToParcelMethod並完成了寫入。 Product(Parcel in)方法永遠不會被調用。最後,我還可以看到我正在啓動新活動的課程進入Pause State
,我的新活動從未創建過。
讓我知道我是否可以提供任何其他信息。
堆棧跟蹤請。 – r2DoesInc 2014-09-03 20:08:30
https://gist.github.com/247842d49577607c77d9.git – srz2 2014-09-04 02:50:27
我一直有同樣的問題人 – 2017-01-29 21:42:23