سؤال

In my app, I am passing a custom object called "Person" from an activity to another. The Person class implements Serializable. I have tried all possible methods shown in StackOverflow, but I'm still getting NullPointerException. On the receiving intent I always get Null when I'm accessing the Person object.

Here is my code:

In Activity FlipCard (Sending Activity):

In onclick of an Edittext:

edt_from.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("RecActvity", "edt_from onclick");
            Intent from_intent = new Intent(FlipCardActivity.this,
                    FromMessageActivity.class);                 
            from_intent.putExtra("From_address", from_address);
            Log.e("Address sending", from_address.getName());
            startActivity(from_intent);

        }
    });

In Activity FromAddress(Receiving Activity):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_from_message);
    Log.e("FromAddress", "OnCreate");
            Person from_address = (Person) getIntent()
            .getSerializableExtra("From_Address");
    if (from_address != null) {
        Log.e("Address", from_address.getName());
    }
   }

Person Class:

public class Person implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1974743341607699233L;
private String name;
private String address1;
private String address2;
private String city;
private String state;
private String postcode;
private String country;

public Person() {
    super();

}

public Person(String name, String addr1, String addr2, String city,
        String state, String pcode, String ctry) {
    this.name = name;
    this.address1 = addr1;
    this.address2 = addr2;
    this.city = city;
    this.state = state;
    this.postcode = pcode;
    this.country = ctry;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPostalAddress() {
    String from_address = this.name + "\n" + this.address1 + "\n"
            + this.address2 + "\n" + this.city + "," + this.state + " "
            + this.postcode + "\n" + this.country;
    return from_address;
}

public String getAddress1() {
    return address1;
}

public void setAddress1(String address1) {
    this.address1 = address1;
}

public String getAddress2() {
    return address2;
}

public void setAddress2(String address2) {
    this.address2 = address2;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getPostcode() {
    return postcode;
}

public void setPostcode(String postcode) {
    this.postcode = postcode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

}

Please help me get rid of this error. I'm not able to figure out what's wrong.

Thanks.

هل كانت مفيدة؟

المحلول

It looks like you have a capitalization problem. In the sending class you have

from_intent.putExtra("From_address", from_address);

in the receiving class you have

.getSerializableExtra("From_Address");

lower-case "a" in "address" in the sending class but upper-case "A" in the receiving class

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top