0

I'm looking for all Internet about how sending a custom object using an Intent inside of an setOnItemClickListener. I want to send this Person Object to another Activity, but it's always sending null. I need to put it in 6 EditText different. Can someone help me?

listEndereco.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(AdrressApp.this, EditorPerson.class);
            Person pessoa = listEndereco.getItemAtPosition(position);
            intent.putExtra("pessoa", pessoa);
            startActivity(intent);
        }
    });

Here is the another activity:

Intent i = getIntent();
Person idPessoa = (Person) i.getSerializableExtra("pessoa");

When I run the app I get this error:

Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
  • Show your `setText` method . – KeLiuyue Jan 08 '18 at 01:14
  • The null pointer exception has nothing to do with send the object via Intent. You need to check on which line the exception is and fix it. I did answer the object via Intent part of your question. If it helps, let me know, or else I can delete that answer. I answered the question without looking at the last line on nullpointer exception. – Henry Jan 08 '18 at 01:20
  • Here my `setText` method. `mEditText.setText(pessoa.getFirstName());` – Gilmar Silva de Santana Jan 08 '18 at 11:51

1 Answers1

1

Person has to be either Serializable or Parcelable for this to work. Also I would suggest you use Parcelable.

Once you implement Parcelable you need to override certain methods. You can refer this to understand how to implement these interfaces. Alternatively you can use something like AutoParcel which does all this for you.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Henry
  • 17,490
  • 7
  • 63
  • 98