0

i created a recycler view which display images and text from Sqlite in listview, To pass the Selected Item name to the New Activity i used Intent to pass data, But When My Intent was Called My App Was Crashed and it Shows Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference

recyclerview

ArrayList<byte[]> list_image;
private LayoutInflater mInflater;
private Context context;
private ArrayList<String> list_name;

 public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

        holder.listname.setText(String.valueOf(list_name.get(position)));

       
        Bitmap bmp = BitmapFactory.decodeByteArray(list_image.get(position), 0, list_image.get(position).length);
        ImageView image = holder.imgname;
        
        image.setImageBitmap(bmp);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(view.getContext(),AddItems.class);
                intent.putExtra("listname", String.valueOf(list_name.get(position)));
                context.startActivity(intent);

            }
        });
    }

Additems

  lisname = findViewById(R.id.listname_dis);

        Intent intent = getIntent();
        String dataTransmited=intent.getStringExtra("listname");
        lisname.setText(dataTransmited);
    }

logcat :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference
        at com.Karthi.check.CustomAdapter$1.onClick(CustomAdapter.java:76)
Kingg
  • 250
  • 1
  • 12
  • use view.getContext() instead of context.startActivity(intent) while starting the activity i.e view.getContext().startActivity(intent) – Gautam Sep 02 '20 at 16:47
  • 1
    Thanks bro, but if i use view.getContext() it Doesnot Perform Any action, If i use view.getContext(intent) then it Show the Error Msg`" expected arguments but found one "` – Kingg Sep 02 '20 at 16:53
  • view.getContext(intent) is incorrect syntax. You have to check AddItems activity than because when you trigger the intent activity will open. – Gautam Sep 02 '20 at 16:56
  • 1
    Pass the context in the constructor of Recycler view from the activity and then use that context. Example in activity RecyclerView rv = new RecyclerView(getApplicationContext()); – Syed Taruf Naqvi Sep 02 '20 at 17:05
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Sep 02 '20 at 19:01

2 Answers2

2

In Your Custom Adapter initlize private Activity activity;

Then

public CustomAdapter(Activity activity,....){

 this.activity = activity;
......
.....
....
}


   use  activity.startActivity(intent); in your OnClickListerner


holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(view.getContext(),AddItems.class);
                intent.putExtra("listname", String.valueOf(list_name.get(position)));
                activity.startActivity(intent);

            }

Also i ur main activity add

CustomAdapter ca = new CustomAdapter(MainActivity.this,this,......)
Karthickyuvan
  • 428
  • 2
  • 16
1

since your "context" is null Try this:

view.getContext().startActivity(intent)

instead of this:

context.startActivity(intent)
Faisal Khan
  • 548
  • 3
  • 16