-1


in my project i work by retrofit and make adapter & model
enter image description here 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);
Cizzl
  • 324
  • 2
  • 11
hmotamed
  • 11
  • 5

2 Answers2

1

you must initialize your context before use it:

public NoteAdapter(List<Querynotes> querynotes, MainActivity mainActivity){
mContext = mainActivity; \\ add this line
    this.querynotes=querynotes;

}
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
0

Your context is null hence the crash. Make sure that you pass a context in the adapters constructor like this :

public NoteAdapter(List<Querynotes> querynotes, Context mContext){

    this.querynotes=querynotes;
    this.mContext=mContext;

}

and in your activity where you are creating the adapter :

NoteAdapter mNoteAdapter = new NoteAdapter(queryNotes, MainActivity.class);
Ahmad Sabeh
  • 526
  • 5
  • 18