可接收接收到的传递数据的可打包对象为null [英] Parcelable object that receives passed data coming up null

查看:69
本文介绍了可接收接收到的传递数据的可打包对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个存储联系人信息的Android应用程序,我试图添加一项功能,当您在ListView中单击某个联系人项时,它将发送实现了第二个活动中的两个以上包裹的联系人对象.

I'm working on an Android app that stores contact information, I'm trying to add a feature that when you click on a contact item in the ListView it will send the contact object that implements parcelable over two the second activity.

这是我将联系人对象发送到联系人活动"的地方. `

This is Where I send the contact object over to the 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类型的对象的那一行是我出现错误的地方,我得到了一个空指针异常.

This is in the onCreate of the Contacts Activity Class, this will check to see if any data was passed using the RECIVED_CONTACT tag. The line where I have an object of type Parcelable is where I have the errors, I'm getting a null pointer exception.

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

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

我的联系人对象看起来像这样

My Contact object looks like this

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对象了解不多,这就是为什么我一直努力做到这一点的原因.

I haven't been able to figure out how to fix this so if you want to see more of my code just ask and I will post it. I don't know much about Parcelable objects and I think that's why I've struggeled to do this.

但是,当我单击MainActivity中的添加按钮时,便能够使用一些EditText字段启动第二个ContactsActivity来添加名字和姓氏,电子邮件和电话,然后将这四个字符串分别放入Contact对象并发送它返回到要显示的主要活动.我没有在此处发布任何代码,因为该代码正在运行.我不知道如何更新ListView中已有的Contact对象之一.

I was however able to when clicking an add button in the MainActivity able to start a second ContactsActivity with some EditText fields for adding first and last names, email and phone I then put each of these four strings into a Contact object and send it back to the Main activity to be displayed. I haven't posted any of that code here because that code is working. I'm not able to figure out how to update one of the Contact objects already in the ListView.

我希望能够将从ListView中选择的联系人对象发送给ContactsActivity,并将四个数据字符串的每一个放入editText字段中.

I want to be able to take the contact object that is selected from the ListView send it to the ContactsActivity and put each of the four strings of data into the editText Fields.

推荐答案

好的,所以当您这样做时:

Okay, so when you're doing this:

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

您最终得到的数据结构如下:

You end up with a data structure something like:

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

以及检索时:

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

您看起来不够深入.您正在Intent.getExtras()捆绑包中查找名为"contact"的字段,但实际上首先需要检索放入它的包含捆绑包:

You're not looking deeply enough. You're looking in the Intent.getExtras() bundle for a field named "contact", but you actually first need to retrieve the containing bundle that you put it in:

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

或者,只需将联系人直接放入Intent的附加功能中即可:

Alternately, just put the contact directly into the Intent's extras:

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

并使用以下内容进行检索:

and retrieve with:

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

这篇关于可接收接收到的传递数据的可打包对象为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆