So I want to send an object (Person) to the Main Activity from another one which is using a recyclerView with a ViewHolder, then close the activity using the viewHolder and print the text on a textView of the Main Activity, I didn't know how to send an object from a viewHolder to another activity so I tried using an interface but I'm getting this error and I don't know how to fix it or what's the cause:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
Which points to this line: obj.SendData_FromViewholder(selectedPerson);
The viewHolder class
class PersonaViewHolder extends RecyclerView.ViewHolder{
public Person selectedPerson;
public ArrayList<SendData_FromViewholder> array = new ArrayList();
public PersonaViewHolder(@NonNull View itemView) {
super(itemView);
ActivityMain actMain = new ActivityMain();
this.addInterfaz(actMain);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedPerson = (Person) v.getTag();
for(SendData_FromViewholder obj : array){
obj.SendData_FromViewholder(selectedPerson);
}
((Activity_PersonList)v.getContext()).finish(); // activity of the recyclerView
}
});
}
public void addInterfaz(SendData_FromViewholder interface){
array.add(interface);
}
}
Main activity with the interface implementation
public class ActivityMain extends AppCompatActivity implements SendData_FromViewholder {
@Override
public void getPersonFromViewholder(Person person) {
TextView textView = findViewById(R.id.tv_mainHeader);
textView.setText(person.getName()+" - ID:"+person.get_id());
}
}
The interface class
public interface SendData_FromViewholder {
void getPersonFromViewholder(Person person);
}