in my project i work by retrofit and make adapter & model
problem its after click on cardview for go to next activity , aplication crashed by error :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:5390)
at ir.hmotamed.notline.adapter.NoteAdapter$1.onClick(NoteAdapter.java:54)
at android.view.View.performClick(View.java:6261)
my adapter code :
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.notesViewHoler> {
List<Querynotes> querynotes;
private Context mContext;
public NoteAdapter(List<Querynotes> querynotes, MainActivity mainActivity){
this.querynotes=querynotes;
}
@NonNull
@Override
public notesViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.note_row,parent,false);
return new notesViewHoler(view);
}
@Override
public void onBindViewHolder(@NonNull final notesViewHoler holder, int position) {
final Querynotes queryPostses=querynotes.get(position);
holder.title.setText(queryPostses.getTitle());
holder.note.setText(queryPostses.getNote());
holder.body.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mContext,EditNoteActivity.class);
intent.putExtra("nid",queryPostses.getId());
intent.putExtra("ntitle",queryPostses.getTitle());
intent.putExtra("ndesc",queryPostses.getNote());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return querynotes.size();
}
public class notesViewHoler extends RecyclerView.ViewHolder{
int id;
CardView parent;
TextView title;
TextView note;
RelativeLayout body;
public notesViewHoler(View itemView) {
super(itemView);
parent=itemView.findViewById(R.id.rv_note_row);
title=itemView.findViewById(R.id.tv_header_title);
note=itemView.findViewById(R.id.tv_body_desc);
body=itemView.findViewById(R.id.rv_row_body);
}
}
i need to after click click on my item go to next activity
but after click my app crashed and get error in line 54 for my adapter
line 54 it's :
Intent intent=new Intent(mContext,EditNoteActivity.class);
you must initialize your context
before use it:
public NoteAdapter(List<Querynotes> querynotes, MainActivity mainActivity){
mContext = mainActivity; \\ add this line
this.querynotes=querynotes;
}