Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Intent.putParcelableArrayListExtra() method and the Parcelable interface, I just want to transfer data in between two activity

Tags:

java

android

Look at my code, wheather wrong in it.

Activity one:

Intent intent = new Intent(SendActivity.this, PickContactsActivity.class);
startActivityForResult(intent, 20);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        ArrayList<PickBean> cons = data.getParcelableArrayListExtra("data");
        for (int i = 0; i < cons.size(); i++) {
            JLog.e(LOG_TAG, "displayName:" + cons.get(i).displayName + "displayNumber" + cons.get(i).displayNumber);
        }
    }
}

Activity two:

Intent data = new Intent();
ArrayList<PickBean> cons = new ArrayList<PickBean>();
for (int i = 0; i < conData.size(); i++) {
        cons.add(new PickBean(conData.get(i).displayName, conData.get(i).displayNumber));
    }
}
data.putParcelableArrayListExtra("data", cons);
setResult(RESULT_OK, data);
finish();

The PickBean code:

public class PickBean implements Parcelable {
    public String displayName;
    public String displayNumber;

    public boolean selected = false;

    public PickBean() {
        super();
    }

    public PickBean(String displayName, String displayNumber) {
        super();
        this.displayName = displayName;
        this.displayNumber = displayNumber;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(displayName);
        dest.writeString(displayNumber);
    }


    public final Parcelable.Creator<PickBean> CREATOR = new Parcelable.Creator<PickBean>() { 
        @Override 
        public PickBean createFromParcel(Parcel source) { 

            return new PickBean(source.readString(), source.readString()); 
        } 

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

It will always throws

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=20,
result=-1, data=Intent { (has extras) }} to activity 
{com.chishacai.smscenter/com.chishacai.smscenter.SendActivity}: java.lang.NullPointerException: expected receiver of type 

com.chishacai.smscenter.bean.PickBean, but got null

Caused by: java.lang.NullPointerException: expected receiver of type 
com.chishacai.smscenter.bean.PickBean, but got null

Please help me how to deal with this problem, thanks.

like image 786
JulyChobits Avatar asked Dec 02 '25 12:12

JulyChobits


1 Answers

Pass the data :

Intent intent = new Intent(SendActivity.this, PickContactsActivity.class); 
Bundle bundle;
bundle.putParcelableArrayList("data", cons); // Be sure con is not null here
intent.putExtras(bundle);

Get the data :

 ArrayList<PickBean> arrayParents = intent.getParcelableArrayListExtra("data");

Replace you method newArray with :

 public final Parcelable.Creator<PickBean> CREATOR = new Parcelable.Creator<PickBean>() { 
        @Override 
        public PickBean createFromParcel(Parcel source) { 

            return new PickBean(source); 
        } 

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

 public PickBean(Parcel data) {

        this.displayName = data.readString();
        this.displayNumber = data.readString();
 }
like image 70
SweetWisher ツ Avatar answered Dec 05 '25 02:12

SweetWisher ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!