我创建了一个DialogFragment。 Dialog有一个EditText,我从对话框xml中调用它。 但是有错误:尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'
此代码中的新内容:
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
和
final EditText editText = (EditText) alertDialog.findViewById(R.id.editTextPasswordDeveloper);
我的代码:
public class DeveloperLoginDialogFragment extends DialogFragment {
EditText editText;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final LayoutInflater inflaterDialog = getActivity().getLayoutInflater();
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
alertDialog = builder.create();
// final AlertDialog alertDialog = new AlertDialog.Builder(getActivity());
builder.setCancelable(false);
builder.setView(inflaterDialog.inflate(R.layout.alertdialog_developer, null));
final EditText editText = (EditText) alertDialog.findViewById(R.id.editTextPasswordDeveloper);
builder.setTitle("Entwickler-Login");
builder.setMessage("Um dich als Entwickler zu verifizieren, gib den Entwickler-Code bitte unten ein.");
builder.setPositiveButton("Verifizieren", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(editText.getText().toString().equals("7846")) {
dialog.dismiss();
Toast.makeText(getActivity().getApplicationContext(),"Erfolgreich verifiziert!", Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
return builder.create();
}
}
答案 0 :(得分:1)
editText = (EditText) editText.findViewById(R.id.editTextPasswordDeveloper);
您正在findViewById()
上呼叫editText
,这是空的。您可能希望改为使用EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
。
请注意使用alertDialog
而不是editText
。
答案 1 :(得分:1)
问题很简单,您正在使用编辑文本来查找视图。你有两个选项可以膨胀布局然后使用findViewById或使用alertDialog.findViewById();
试试这个。
AlertDialog.Builder builder = new AlertDialog.Builder(FeedFragment.this.getActivity());
builder.setView(R.layout.layout_edit_review);
builder.setCancelable(true);
Dialog dialog = builder.create();
dialog.show();
TextInputEditText text_edit = (TextInputEditText) dialog.findViewById(R.id.editReview);
尝试类似的东西。