簡體   English   中英

接收傳遞的數據的可打包對象為null

[英]Parcelable object that receives passed data coming up null

我正在使用一個存儲聯系人信息的Android應用程序,正在嘗試添加一項功能,當您在ListView中單擊某個聯系人項時,它將發送在第二個活動中實現可拆分的聯系人對象。

這是我將聯系人對象發送到Contacts Activity的地方。 `

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    final Contact item = (Contact) getListAdapter().getItem(position);

    Intent updateIntent = new Intent(MainActivity.this, ContactsActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable("contact", item);
    updateIntent.putExtra(ContactsActivity.RECIVED_CONTACT, bundle);
    startActivityForResult(updateIntent,1);
    adapter.notifyDataSetChanged();
}`

這在Contacts Activity類的onCreate中,它將檢查是否使用RECIVED_CONTACT標記傳遞了任何數據。 我有Parcelable類型的對象的行就是我發生錯誤的行,我得到了空指針異常。

if (getIntent().getParcelableExtra(ContactsActivity.RECIVED_CONTACT) != null) {

    //This line is where the exception ouccurs
    Parcelable receivedContact = getIntent().getParcelableExtra("contact"); 
    updateContact(receivedContact);
}

我的聯系人對象看起來像這樣

public class Contact implements Parcelable {

String firstName;
String lastName;
String email;
String phone;

/**
 * Constructor
 */
public Contact(String firstName, String lastName, String email, String phone) {
    // TODO Auto-generated constructor stub
    this();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.phone = phone;
}

public Contact(Parcel parcel) {
    this.firstName = parcel.readString();
    this.lastName = parcel.readString();
    this.email = parcel.readString();
    this.phone = parcel.readString();
}

 // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() {
        public Contact createFromParcel(Parcel in) {
            //return new Contact(in);
            Contact mContact = new Contact();
            mContact.firstName = in.readString();
            mContact.lastName = in.readString();
            mContact.email = in.readString();
            mContact.phone = in.readString();
            return mContact;
        }

        public Contact[] newArray(int size) {
            return new Contact[size];
        }

    };

    public Contact() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    public String toString() {
        return firstName + " \n" + lastName + " \n"
                + email + " \n" + phone + " \n\n";
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(firstName);
        out.writeString(lastName);
        out.writeString(email);
        out.writeString(phone);
    }

}

我還無法弄清楚如何解決此問題,因此,如果您想查看更多我的代碼,請詢問,我將其發布。 我對Parcelable對象了解不多,這就是為什么我一直努力做到這一點的原因。

但是,當我單擊MainActivity中的添加按鈕時,便能夠使用一些EditText字段啟動第二個ContactsActivity來添加名字和姓氏,電子郵件和電話,然后將這四個字符串中的每一個放入Contact對象中,然后將其發送回要顯示的主要活動。 我沒有在此處發布任何代碼,因為該代碼正在運行。 我無法弄清楚如何更新ListView中已有的Contact對象之一。

我希望能夠將從ListView中選擇的聯系人對象發送到ContactsActivity,並將四個數據字符串的每一個放入editText字段中。

好的,所以當您這樣做時:

Intent updateIntent = new Intent(MainActivity.this, ContactsActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("contact", item);
updateIntent.putExtra(ContactsActivity.RECIVED_CONTACT, bundle);
startActivityForResult(updateIntent,1);
adapter.notifyDataSetChanged();

您最終得到的數據結構如下:

Intent {
    Bundle extras {
        Bundle bundle {
            Contact contact;
        }
    }
}

當您檢索它時:

Parcelable receivedContact = getIntent().getParcelableExtra("contact"); 

您看起來不夠深入。 您正在Intent.getExtras()包中查找名為“ contact”的字段,但實際上首先需要檢索放入它的包含包:

Bundle bundle = getIntent().getBundleExtra(ContactsActivity.RECEIVED_CONTACT);
Contact contact = bundle.getParcelableExtra("contact");

或者,只需將聯系人直接放入Intent的其他功能中:

Intent updateIntent = new Intent(MainActivity.this, ContactsActivity.class);
updateIntent.putExtra(ContactsActivity.RECEIVED_CONTACT, item);

並使用:

Contact contact = getIntent().getParcelableExtra(ContactsActivity.RECEIVED_CONTACT);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM