Android活动通过intent返回null

时间:2015-06-01 00:25:38

标签: java android android-intent serializable

当我尝试从另一个活动(通过Intents完成)将数据返回到Main活动时,不确定为什么我一直得到一个空引用。我尝试过序列化所有内容,并尝试了其他内容。我不知道可能导致它的原因。有人可以指出我的错误吗?

以下是我不断收到的错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=0, data=null} to activity {ebadly.com.youstreamer/ebadly.com.youstreamer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference

以下是我的MainActivity类中的代码:

public static final int PICK_CONTACTS = 10;
    public ArrayList<Contact> mSendPhoneNumbers;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSendPhoneNumbers = new ArrayList<Contact>();
        Button contactsButton = (Button)findViewById(R.id.select_contacts_button);
        contactsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, ContactsListActivity.class);
                i.putExtra(ContactsListActivity.EXTRA, mSendPhoneNumbers);
                startActivityForResult(i, PICK_CONTACTS);
            }
        });

        Button enterButton = (Button)findViewById(R.id.enter_button);
        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
enter code here

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mSendPhoneNumbers = (ArrayList<Contact>)data.getSerializableExtra(ContactsListActivity.EXTRA);

    }

以下是我的ContactsListActivity类的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts_list);

        mSendPhoneNumbers = new ArrayList<Contact>();
        mSendPhoneNumbers = (ArrayList<Contact>) getIntent().getSerializableExtra(EXTRA);

        mContacts = new ArrayList<Contact>();
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (phones.moveToNext())
        {
            Contact c = new Contact();
            c.mName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            c.mNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            mContacts.add(c);
        }
        phones.close();

        if(!mSendPhoneNumbers.isEmpty() || mSendPhoneNumbers != null){
            for(Contact c : mSendPhoneNumbers){
                if(c.mChecked == true){
                    for(Contact search: mContacts){
                        if(search.mNumber.equals(c.mNumber)){
                            search.mChecked = true;
                        }
                    }
                }
            }
        }

        mContactsList = (ListView) findViewById(R.id.contact_list);
        mContactsList.setAdapter(new ContactListViewAdapter(mContacts));

        mContactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Contact c = (Contact) parent.getAdapter().getItem(position);
                if(c.mChecked == true) c.mChecked = false;
                else c.mChecked = true;
            }
        });
    }

@Override
    public void onBackPressed(){
        super.onBackPressed();
        for(Contact addContact: mContacts){
            if (addContact.mChecked){
                for(Contact search : mSendPhoneNumbers){
                    if(search.mNumber.equals(addContact.mNumber)){
                        break;
                    }else mSendPhoneNumbers.add(addContact);
                }
            }
        }
        Intent i = new Intent();
        Log.d("HEREEE === ", mSendPhoneNumbers.toString());
        i.putExtra(EXTRA, mSendPhoneNumbers);
        setResult(RESULT_OK, i);
        finish();
    }

0 个答案:

没有答案