DialogFragment 中的 NullPointerException
NullPointerException in DialogFragment
我在单击 DialogFragment
的肯定按钮时遇到了 NullPointerException 出现的问题。我设置了一个主要由 EditText 组成的布局,我在我的应用程序中使用了它们的内容。当应用程序尝试检索 EditText
的内容时出现问题
在我的 MainActivity
中,我在 Button
上设置了一个侦听器,它使用 show()
方法调用 DialogFragment
。
这是描述我的 DialogFragment
:
的代码片段
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控制器时请使用这个
yourview.findViewById(R.id.name_options);
使用要传递给 setView
的 View
对象从 Dialog
的布局访问 EditText
。这样做:
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
....
在 onClick 方法中获取 Dialog 的 EditText:
EditText name_options = (EditText)view. findViewById(R.id.name_options);
如果行
String text = name_options.getText().toString();
导致 NullPointerException 那么 name_options
可能为空,所以可能在您的布局中没有带有 name_options
id 的 EditText。
EditText name_options = (EditText) findViewById(R.id.name_options);
这可能 returns 无效。
尝试使用 getActivity().findViewById(R.id.name_options)
或传递包含 name_options
视图的 context/view
改变
EditText name_options = (EditText)findViewById(R.id.name_options);
到
EditText name_options = (EditText)getactivity().findViewById(R.id.name_options);
我认为你应该使用这个:
EditText name_options = (EditText) getDialog().findViewById(R.id.name_options);
我在单击 DialogFragment
的肯定按钮时遇到了 NullPointerException 出现的问题。我设置了一个主要由 EditText 组成的布局,我在我的应用程序中使用了它们的内容。当应用程序尝试检索 EditText
在我的 MainActivity
中,我在 Button
上设置了一个侦听器,它使用 show()
方法调用 DialogFragment
。
这是描述我的 DialogFragment
:
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控制器时请使用这个
yourview.findViewById(R.id.name_options);
使用要传递给 setView
的 View
对象从 Dialog
的布局访问 EditText
。这样做:
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
....
在 onClick 方法中获取 Dialog 的 EditText:
EditText name_options = (EditText)view. findViewById(R.id.name_options);
如果行
String text = name_options.getText().toString();
导致 NullPointerException 那么 name_options
可能为空,所以可能在您的布局中没有带有 name_options
id 的 EditText。
EditText name_options = (EditText) findViewById(R.id.name_options);
这可能 returns 无效。
尝试使用 getActivity().findViewById(R.id.name_options)
或传递包含 name_options
视图的 context/view
改变
EditText name_options = (EditText)findViewById(R.id.name_options);
到
EditText name_options = (EditText)getactivity().findViewById(R.id.name_options);
我认为你应该使用这个:
EditText name_options = (EditText) getDialog().findViewById(R.id.name_options);