简体   繁体   中英

Android: Parcelable returns nothing

I have the following problem: I want to parcel one object from one activity to the other.

It is this object:

public class Item implements Parcelable {

private int minimumAmountOfItems;
private String nameOfItem;
private int maximumAmountOfItems;
private int actualAmountOfItems;

public Item(int minimumAmountOfItems, String nameOfItem, int     maximumAmountOfItems, int actualAmountOfItems){
    this.minimumAmountOfItems = minimumAmountOfItems;
    this.nameOfItem = nameOfItem;
    this.maximumAmountOfItems = maximumAmountOfItems;
    this.actualAmountOfItems = actualAmountOfItems;
}

With the following parcelable coding stuff:

 @Override
public int describeContents() {
    /*Necessary for the parcelable stuff; but not needed in about 99.9% of the cases (source: Stack Overflow)*/
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    /*Store the data in a parcelable object to communicate between Activities */
    dest.writeInt(this.minimumAmountOfItems);
    dest.writeString(this.nameOfItem);
    dest.writeInt(this.maximumAmountOfItems);
    dest.writeInt(this.actualAmountOfItems);

}

private Item(Parcel in){

    /*Retrieve the data that was packaged before*/

    this.minimumAmountOfItems = in.readInt();
    this.nameOfItem = in.readString();
    this.maximumAmountOfItems = in.readInt();
    this.actualAmountOfItems = in.readInt();
}

public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>(){
    /*This small part of code passes along the unmarshalled parcel and returns the next object.*/
    @Override
    public Item createFromParcel(Parcel in){
        return new Item(in);
    }

    @Override
    public Item[] newArray(int size){
       return new Item[size];
    }
};

Therefore, I use in my to activities the following code to parcel my object:

resultIntent = new Intent(ItemCreatorActivity.this, ItemListEditorActivity.class);
resultIntent.putExtra("Item", item);
startActivity(resultIntent);

And to get everything I want to implement this:

Intent inputIntent = getIntent();
   Item passedItem = inputIntent.getExtras().getParcelable("Item");
System.out.println(passedItem.getNameOfItem());

Now I have several problems:

1.) How can I check, whether I get valid parcelable information from my sender to my receiver? I think that I have got everything important about parcelable objects from this tutorial: https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android several others (due to readability purposes I won't post them here, because they all describe the same procedure and structure...); but still my System.out.println(passedItem.getNameOfItem()) throws nothing. And there are no further messages in my AndroidMonitor.

2.)I am calling this activity one time, when I do not have created an Item object. Is there a more "smooth" way to prevent a nullpointerexception in this code for the very first call of the activity?

3.) When I am pressing on a button, switching to a new Activity, creating an Item object and imagine, my code would have worked before and I could pass my object back to the calling Activity. Will all objects, that I have saved before in an ArrayList from the calling Activity, still be available or do I have to pass my ArrayList to the new Activity and then back to the calling Activity?

Thank you for your help in advance.

Best regards

You are adding parcelable object in intent and retrieving it from Bundle (inputIntent.getExtras() returns object of type Bundle).

So you need to change it to inputIntent.getParcelableExtra("Item");

3.) When I am pressing on a button, switching to a new Activity, creating an Item object and imagine, my code would have worked before and I could pass my object back to the calling Activity. Will all objects, that I have saved before in an ArrayList from the calling Activity, still be available or do I have to pass my ArrayList to the new Activity and then back to the calling Activity?

Yes. You can add ArrayList to intent object:

ArrayList<Item> itemList = new ArrayList<Item>();
// Add elements to the list
intent.putParcelableArrayListExtra("Item_List", itemList);

And retrieve it:

ArrayList<Item> itemList = inputIntent.getParcelableArrayListExtra("Item_List");

1.) How can I check, whether I get valid parcelable information from my sender to my receiver? I think that I have got everything important about parcelable objects from this tutorial: https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android several others (due to readability purposes I won't post them here, because they all describe the same procedure and structure...); but still my System.out.println(passedItem.getNameOfItem()) throws nothing. And there are no further messages in my AndroidMonitor.

You can log to your logcat by doing

Log.d("tag", passedItem.getNameOfItem()); 

and view your logcat. It should print out your data

2.)I am calling this activity one time, when I do not have created an Item object. Is there a more "smooth" way to prevent a nullpointerexception in this code for the very first call of the activity?

You can do this in your receiving activity:

Bundle extras = getIntent().getExtras();
if (extras != null) {
   Item passedItem = (Item) extras.getParcelable("Item"); 
}

3.) When I am pressing on a button, switching to a new Activity, creating an Item object and imagine, my code would have worked before and I could pass my object back to the calling Activity. Will all objects, that I have saved before in an ArrayList from the calling Activity, still be available or do I have to pass my ArrayList to the new Activity and then back to the calling Activity?

As for this, I would look into http://developer.android.com/training/basics/activity-lifecycle/recreating.html that will teach you about recreating activities and saving data through states.

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