gpt4 book ai didi

Android 从 Fragment 中的 AlertDialog 获取 EditText 值

转载 作者:行者123 更新时间:2023-11-29 19:59:54 24 4
gpt4 key购买 nike

单击“确定”按钮时出现空指针异常。我正在尝试从 AlertDialog 中的 EditText 获取值。我正在从一个 fragment 中调用 AlertDialog。

我相信这条线是问题所在查看promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);我认为这是因为我正在传递 null 但我不确定要传递哪些元素。请注意,我是从 fragment 而不是 Activity 调用 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());
}
}

如有任何帮助或建议,我们将不胜感激。谢谢

最佳答案

我认为你应该在 promptView 而不是 View 变量上调用 findViewById

关于Android 从 Fragment 中的 AlertDialog 获取 EditText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36440393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com