gpt4 book ai didi

android - DialogFragment 中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 14:47:59 25 4
gpt4 key购买 nike

当我单击 DialogFragment 的肯定按钮时,我遇到了 NullPointerException 出现的问题。我设置了一个主要由 EditText 组成的布局,我在我的应用程序中使用了它们的内容。当应用程序尝试检索 EditText

的内容时出现问题

在我的 MainActivity 中,我在 Button 上设置了一个 Listener,它使用 show() 调用 DialogFragment > 方法。这是描述我的 DialogFragment 的代码 fragment :

private class PersonalInfoDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(inflater.inflate(R.layout.personalinformation_dialog, null))
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

EditText name_options = (EditText) findViewById(R.id.name_options);
//This line makes the app crash:
String text = name_options.getText().toString();
//doing some job...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}

为什么会出现这种异常?

最佳答案

兄弟..当你初始化你的xml Controller 时请使用这个

yourview.findViewById(R.id.name_options);

关于android - DialogFragment 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28767080/

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