2014-09-03 69 views
2

我有一個Android應用程序,實現了Parcelable接口的自定義對象。他們設置它的方式是,我的程序最初從包中的文件創建自定義類ProductsArrayList。我可以看到並確認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,我的新活動從未創建過。

讓我知道我是否可以提供任何其他信息。

+0

堆棧跟蹤請。 – r2DoesInc 2014-09-03 20:08:30

+0

https://gist.github.com/247842d49577607c77d9.git – srz2 2014-09-04 02:50:27

+0

我一直有同樣的問題人 – 2017-01-29 21:42:23

回答

0

這些似乎是由於我的應用程序用作資源的一些格式不正確的XML。不知道爲什麼會出現這個問題,但經過數小時的查找後,我能夠刪除不好的XML,並在稍後需要發佈應用程序時再次訪問此問題。

現在,我只是會擔心繼續發展它。如果我發現有關XML的任何有趣內容,我會盡量記得在這裏查看。

0

相當確定您的問題是使用writeList()writeList()似乎表明它遵循與列表中包含的項目相同的合同writeValue()。然而,readList()似乎表明值必須是Parcelable(其中String不是)。無論哪種方式,通常這些呼叫必須非常特定地與其反相關聯(例如,與其相反)。writeString()必須閱讀與readString(),不readValue()),所以你應該使用所提供的方法進行讀/寫StringList S:

// Takes in a List<String> which may be null 
out.writeStringList(similarItems); 

// Returns either null or a new ArrayList<String> with the contents 
similarItems = in.createStringArrayList(); 
+0

雖然這不是每個實例都是這樣嗎?我從我的XML資源中爲數百個條目使用此方法,它們都使用上面列出的方法(除了一個之外)。那麼這不就是一個不好的資源條目嗎? – srz2 2014-09-18 20:55:54

+0

是的,如果它不會在其他地方引發問題,這很有趣。進一步研究它看起來像'writeList'和'readArrayList'也應該正常工作(雖然它不是類型安全的,因爲它只是返回一個'ArrayList')。不過,不應該有任何可以放入'ArrayList '的東西,這會導致類似的崩潰。你能從崩潰中捕獲一個日誌,並把它放在Github Gist的其他地方嗎?它不會爲我加載。 (也許是pastebin?) – kcoppock 2014-09-18 21:00:02