从活动传递时将对象设为null

时间:2014-02-03 03:28:35

标签: android object android-intent nullpointerexception serializable

在我的应用程序中,我将一个名为“Person”的自定义对象从一个活动传递给另一个。 Person类实现Serializable。我已经尝试了StackOverflow中显示的所有可能的方法,但我仍然得到NullPointerException。在接收意图时,当我访问Person对象时,我总是得到Null。

这是我的代码:

在活动活动挂牌(发送活动)中:

点击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);

        }
    });

在活动FromAddress(接收活动)中:

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

人类:

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

}

请帮我摆脱这个错误。我无法弄清楚出了什么问题。

感谢。

1 个答案:

答案 0 :(得分:2)

看起来你有大写问题。在发送课程中你有

from_intent.putExtra("From_address", from_address);

在接收课程中

.getSerializableExtra("From_Address");

发送类中“地址”中的小写“a”,但接收类中的大写“A”