我的计划是从DialogFragment中的EditText获取Tutor的LastName
,FirstName
和MiddleName
。这是我的DialogFragment的代码:
public class AddInstructorDialog extends android.app.DialogFragment
{
DatabaseHelper myDb;
View myView;
public AlertDialog.Builder builder;
private EditText tutorLastName, tutorFirstName, tutorMiddleName;
private dialogListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
myDb = new DatabaseHelper(getActivity());
View mView = getActivity().getLayoutInflater().inflate(R.layout.alert_dialog_add_instructor, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Add Instructors")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
myDb.getInstructorNumber();
if(myDb.instructorNumber == null)
{
Toast.makeText(getActivity(), "IN1", Toast.LENGTH_SHORT).show();
}
else
{
String sum = myDb.instructorNumber;
String total = "IN" + sum;
EditText txtLastName = myView.findViewById(R.id.tutor_LastName);
EditText txtMiddleName = myView.findViewById(R.id.tutor_MiddleName);
EditText txtFirstName = myView.findViewById(R.id.tutor_FirstName);
String LastName = txtLastName.getText().toString();
String MiddleName = txtMiddleName.getText().toString();
String FirstName = txtFirstName.getText().toString();
listener.applyValuse(LastName, FirstName, MiddleName);
String fullName = LastName + ", " + FirstName + LastName;
boolean isInserted = myDb.addInstructorData(
total,
LastName,
FirstName,
MiddleName,
fullName
);
if(isInserted == true)
{
Toast.makeText(getActivity(), "Inserted", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(), "Not inserted", Toast.LENGTH_SHORT).show();
}
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
myDb.insertInsData();
}
});
builder.setView(mView);
AlertDialog dialog = builder.create();
dialog.show();
return dialog;
}
获取instructorID
的值没有问题。我的问题是,在获得instructorID
的值之后,它将继续从DialogBox的EditText获取输入的数据,应用程序将崩溃。它不读取
EditText txtLastName = myView.findViewById(R.id.tutor_LastName);
在该行中,应用程序将崩溃。
我错过了什么吗?我尝试移动那条线,但它不起作用。