Reputation: 859
I am not very advanced developer. Currently I am working on an app in which the alertdialog has to be called from a clickable image in a card. I developed it like this:
public class patient_detailsAdapter extends RecyclerView.Adapter<patient_detailsAdapter.patientDetailsViewHolder>{
ArrayList<patient_details> patientDetailList = new ArrayList<patient_details>();
Context mContext;
public patient_detailsAdapter(ArrayList<patient_details> patientDetailList, Context mContext){
this.patientDetailList = patientDetailList;
this.mContext = mContext;
}
@Override
public patientDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.patient_list_layout,parent,false);
patientDetailsViewHolder patientDetailsViewHolder = new patientDetailsViewHolder(view);
return patientDetailsViewHolder;
}
@Override
public void onBindViewHolder(final patientDetailsViewHolder holder, int position) {
patient_details patient = patientDetailList.get(position);
holder.person_img.setImageResource(patient.getPatientImage());
holder.person_name.setText(patient.getName());
holder.person_email.setText(patient.getEmail());
holder.callButtonImage.setImageResource(patient.getCallButtonImage());
holder.callButtonImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(mContext)
.setTitle("ABC")
.setMessage("bhnshsks")
.setIcon(R.drawable.call_now)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Result","Success");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Result","Success");
}
})
.show();
}
});
}
@Override
public int getItemCount() {
return patientDetailList.size();
}
public static class patientDetailsViewHolder extends RecyclerView.ViewHolder{
ImageView person_img,callButtonImage;
TextView person_email,person_name;
public patientDetailsViewHolder(View view){
super(view);
person_img = (ImageView) view.findViewById(R.id.profile_image);
person_email = (TextView) view.findViewById(R.id.profile_email);
person_name = (TextView) view.findViewById(R.id.profile_name);
callButtonImage = (ImageView) view.findViewById(R.id.callButton);
}
}
}
But it is showing error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
And for using v7 widget of alert dialog, I am giving theme to it:
holder.callButtonImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
});
Then I got the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
But the main thing is that, when I removed alert dialog, and tested onclickListener with Log.i, I got success.
Please help, I searched everywhere but found nothing useful. Thanks in advance.
Edits
My activities are:
public class PatientListActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<patient_details> list ;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
list = new ArrayList<patient_details>();
preparValue();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new patient_detailsAdapter(list,context);
recyclerView.setAdapter(adapter);
}private void preparValue(){
int[] images = new int[]{ R.drawable.abc, R.drawable.efg, R.drawable.efr};
int[] buttonImage = new int[]{R.drawable.call_now};
patient_details a = new patient_details(images[0],"[email protected]","ABC_Name",buttonImage[0]);
list.add(a);
patient_details b = new patient_details(images[1],"[email protected]","EFG_Name",buttonImage[0]);
list.add(b);
patient_details c = new patient_details(images[2],"[email protected]","Leonardo Di Caprio",buttonImage[0]);
list.add(c);
}
It's my activity where I instantiated the class.
Upvotes: 0
Views: 1040
Reputation: 8254
The context that you passed in the adapter constructor is null.
Your Activity extends Context, so you should do in your PatientListActivity
:
adapter = new patient_detailsAdapter(list,this);
Instead of:
adapter = new patient_detailsAdapter(list,context);
Upvotes: 2