在自定义对话框中显示 Toast 但无法读取上下文
Display Toast in custom dialog but context cannot be read
我正在尝试创建一个自定义对话框,当用户单击肯定按钮时它会显示祝酒词,但我在 Toast 行上遇到错误。问题是什么以及如何解决?我认为这与上下文有关。错误只是说:无法解析方法 '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();
}
}
Toast.makeText(view.getContext(), subjectListModel.toString(), Toast.LENGTH_SHORT).show();
要传递的值是上下文。
您传递了错误的值,如果您传递了 'SubjectListDialog.this',您传递的是 SubjectListDialog class 本身,而不是上下文。
Toast.makeText(requireContext(), subjectListModel.toString(),Toast.LENGTH_SHORT).show();
我正在尝试创建一个自定义对话框,当用户单击肯定按钮时它会显示祝酒词,但我在 Toast 行上遇到错误。问题是什么以及如何解决?我认为这与上下文有关。错误只是说:无法解析方法 '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();
}
}
Toast.makeText(view.getContext(), subjectListModel.toString(), Toast.LENGTH_SHORT).show();
要传递的值是上下文。
您传递了错误的值,如果您传递了 'SubjectListDialog.this',您传递的是 SubjectListDialog class 本身,而不是上下文。
Toast.makeText(requireContext(), subjectListModel.toString(),Toast.LENGTH_SHORT).show();