I'm trying to create a custom dialog where it will show a toast when the user click on the positive button, but I got an error on the Toast line. What is the problem and how to resolve it? I think it has something to do with the context. The error just says: Cannot resolve method 'makeText(com.example.studentsacademicmanagementappsama.SubjectListDialog, java.lang.String, int)'
SubjectListDialog.java
public class SubjectListDialog extends AppCompatDialogFragment {
private EditText et_subjectCode, et_subjectName, et_creditHour;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_subjectlist_dialog, null);
builder.setView(view)
.setTitle("Add Subject")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
SubjectListModel subjectListModel = new SubjectListModel(et_subjectCode.
getText().toString(),et_subjectName.getText().toString(),
Integer.parseInt(et_creditHour.getText().toString()));
Toast.makeText(SubjectListDialog.this, subjectListModel.toString(),
Toast.LENGTH_SHORT).show();
}catch (Exception e){
}
}
});
et_subjectCode = view.findViewById(R.id.et_subjectCode);
et_subjectName = view.findViewById(R.id.et_subjectName);
et_creditHour = view.findViewById(R.id.et_creditHour);
return builder.create();
}
}
CodePudding user response:
The value to pass is the context.
You are passing the wrong value, if you pass 'SubjectListDialog.this' you are passing the SubjectListDialog class itself, not the context.
Toast.makeText(requireContext(), subjectListModel.toString(),Toast.LENGTH_SHORT).show();
CodePudding user response:
Toast.makeText(view.getContext(), subjectListModel.toString(), Toast.LENGTH_SHORT).show();