简体   繁体   中英

Android: Custom Parcelable Class Crashes App Without Throwing Exception

I've got an Android app with custom objects which implement the Parcelable interface. They way I have it set it up is that my program initially creates an ArrayList of a custom class Products from a file in the bundle. I can see and confirm that the arraylist and it's instance variabels are populated appropriately. This class has several instance variables along with one being another ArrayList but with the String class. Remember that fact.

I am trying to pass the ArrayList<Product> into a new activity like so:

  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());
       }

I am collecting the information back from the intent in my new activity by pulling the ArrayList out by using

     app_products = getIntent().getParcelableArrayListExtra("Products");

For my custom class, it looks something like this, along with the implemented Parcelable methods.

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);
    }
}

So this works well my String arraylist being added in the class 我的String arraylist被添加到类中

Comment out out.writeList(similarItems); and also similarItems = in.readArrayList(String.class.getClassLoader());

but once you add them back in into the class, the app crashes but it doesn't even throw a message for debugging. I've wrapped everything around try-catch statements and android doesn't even report the app crashed with the normal dialog on the springboard. I am truly at a loss.

It is worth mentioning that I've used some log statements to understand where the program is crashing despite the fact that android wont throw an exception. I can see that all of the items in my ArrayList undergoes the writeToParcelMethod and completes writing. The Product(Parcel in) method is never called. Lastly, I can also see the class I am launching the new activity from enters the Pause State and my new Activity is never created.

Let me know if I can provide any additional information.

Fairly certain your problem is the use of writeList() . writeList() seems to indicate that it follows the same contract as writeValue() for the items contained in the list. readList() however, seems to indicate that the values must be Parcelable (which String is not).

Either way, typically these calls have to be very specifically linked to their inverse (eg writeString() must be read in with readString() , not readValue() ) so you should instead use the provided methods for reading/writing String List 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();

These seemed to be due to some malformed XML which my app uses as a resource. Not sure why this was this issue but after many hours of hunting it down, I was able to remove the bad XML and will revisit this issue at a later date towards when I need to release the app.

Right now, I'm just gonna worry about continuing to develop it. I'll try to remember to check back here if I find anything interesting about my XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM