Android从片段中的AlertDialog获取EditText值

时间:2016-04-06 02:26:17

标签: android android-fragments fragment alertdialog

单击“确定”按钮时,我收到空指针异常。我试图从AlertDialog中的EditText获取值。我从片段中调用AlertDialog。

我相信这条线是问题所在 查看promptView = layoutInflater.inflate(R.layout.passchange_dialog,null); 我认为这是因为我传递了null,但我不确定要传递的是什么。请注意我从片段而不是活动中调用AlertDialog。

public class fragment_account extends myFragment {

private final String firebase = "https://xxxxxxxx.firebaseio.com";
private String email = "";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_account, container, false);

    SharedPreferences sp = getActivity().getPreferences(0);
    TextView address = (TextView) view.findViewById(R.id.textViewEmailAddress);
    email = sp.getString("email", "");
    address.setText(email);

    ImageView imageChangePass = (ImageView) view.findViewById(R.id.imageChangePass);
    imageChangePass.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v) {
                    showInputDialog(v);
                }
            }
    );
    return view;
}

protected void showInputDialog(final View view) {

    try
    {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

        View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext());
        alertDialogBuilder.setView(promptView);


        alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        EditText edittextOldPass = (EditText) view.findViewById(R.id.edittextOldPass);
                        EditText edittextNewPass = (EditText) view.findViewById(R.id.edittextNewPass);
                        EditText edittextConfirmPass = (EditText) view.findViewById(R.id.edittextConfirmPass);
                        final String oldPass = edittextOldPass.getText().toString().trim();
                        final String newPass = edittextNewPass.getText().toString().trim();
                        final String confirmPass = edittextConfirmPass.getText().toString().trim();

                        Firebase ref = new Firebase(firebase);
                        ref.changePassword(email, oldPass, newPass, new Firebase.ResultHandler() {
                            @Override
                            public void onSuccess() {
                            }

                            @Override
                            public void onError(FirebaseError firebaseError) {
                            }
                        });
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }
    catch (Exception e)
    {
        Log.i("myStuff", e.getMessage());
    }
}

}

非常感谢任何帮助或建议。感谢

2 个答案:

答案 0 :(得分:2)

我认为你应该在promptView而不是在视图变量

上调用findViewById

答案 1 :(得分:0)

我猜你应该在设置对话框的布局之前初始化所有textview,而不是在onClick方法中。由于您必须访问它们的值,因此您必须将它们声明为final,以便获取用户在onClick函数内输入的内容。