小部件初始化崩溃

时间:2018-02-19 10:28:55

标签: android dialog initialization android-widget dialogfragment

我的计划是从DialogFragment中的EditText获取Tutor的LastNameFirstNameMiddleName。这是我的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);

在该行中,应用程序将崩溃。

我错过了什么吗?我尝试移动那条线,但它不起作用。

0 个答案:

没有答案