I am a beginner who just has started to learn Android. I am currently stuck with a problem that I am unable to solve
I have to made an interface that is going to send a recyclerview position(int) from the Adapter to the mainActivity
Here is my interface
public interface OnButtonClicked {
public void On_Button_Cancel_Clicked(int position);
}
Here is my Adapter
public class AppointmentAdapter extends RecyclerView.Adapter<AppointmentAdapter.SenderViewHolder> {
ArrayList<Users> list;
Context context;
OnButtonClicked onButtonClicked;
public AppointmentAdapter(ArrayList<Users> list, Context context, OnButtonClicked onButtonClicked) {
this.list = list;
this.context = context;
this.onButtonClicked = onButtonClicked;
}
@NonNull
@Override
public SenderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.appointment_card, parent, false);
return new SenderViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SenderViewHolder holder, int position) {
Users users = list.get(position);
holder.username.setText(users.getUsername());
holder.token_no.setText("!2:00");
holder.time.setText("12:00");
Picasso.get().load(users.getProfile_pic()).placeholder(R.drawable.ic_impatient).into(holder.image);
holder.call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+ users.getMobile()));
v.getContext().startActivity(intent);
}
});
holder.Camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here is the interface position i am trying to pass
String s = users.getId();
Appointment obj = new Appointment();
obj.On_Button_Cancel_Clicked(holder.getLayoutPosition());//<--this is my //problem i am trying to send adapter position to main activity and when i run the code the //and try to print the layout position i am getting the output in the android terminal but //my app crashes
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class SenderViewHolder extends RecyclerView.ViewHolder{
CircleImageView image;
TextView username, token_no, time;
Button call, cancel, Camera;
LinearLayout linear_layout;
OnButtonClicked onButtonClicked;
public SenderViewHolder(@NonNull View itemView) {
super(itemView);
image = (CircleImageView) itemView.findViewById(R.id.app_user_image);
username = (TextView) itemView.findViewById(R.id.app_Username);
token_no = (TextView) itemView.findViewById(R.id.app_token);
time = (TextView) itemView.findViewById(R.id.app_time);
call = (Button) itemView.findViewById(R.id.app_call);
cancel = (Button) itemView.findViewById(R.id.app_close);
Camera = (Button) itemView.findViewById(R.id.app_Take_picture);
}
}
}
Here is my main Activity Appointment.java
I've initialized the constructor
I implemented the interface in my main activity
public class Appointment extends AppCompatActivity implements OnButtonClicked {
AppointmentAdapter appointmentAdapter = new AppointmentAdapter(messageModels, getApplicationContext(), this::On_Button_Cancel_Clicked);
public void On_Button_Cancel_Clicked(int position) {
System.out.println(position);
}
//when I try to print the position in my main activity using "System.out.println()" the program runs fine and the app wont crash 1.)Outputs when i use Sout I/System.out: 0 ->on clicking position 0 I/System.out: 1 ->on clicking position 1
but when I try to make a Toast in the interface here my app crashes
public void On_Button_Cancel_Clicked(int position) {
Toast.makeText(activity, position, Toast.LENGTH_SHORT).show();
}
}
the error is
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nowait, PID: 12735 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.widget.Toast.makeText(Toast.java:315) at com.example.nowait.Appointment.On_Button_Cancel_Clicked(Appointment.java:242)<-- at com.example.nowait.AppointmentAdapter$2.onClick(AppointmentAdapter.java:76)<-- at android.view.View.performClick(View.java:7185) at android.view.View.performClickInternal(View.java:7162) at android.view.View.access$3500(View.java:819) at android.view.View$PerformClick.run(View.java:27684) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:224) at android.app.ActivityThread.main(ActivityThread.java:7590) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
AppointmnetAdapter.java:76 obj.On_Button_Cancel_Clicked(holder.getLayoutPosition());
Appointment.java:242 Toast.makeText(Appointment.this, position, Toast.LENGTH_SHORT).show();
please help me i am struck with this for 4 days