这是我的代码:
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
final EditText a = (EditText) alert.findViewById(R.id.kekekeke);
LayoutInflater inflater = getLayoutInflater();
alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
alertDialogBuilder
.setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
});
}
我的问题是我遇到了以下错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'
我很困惑,因为我认为我已经通过edittext
访问了它。
答案 0 :(得分:5)
您可以像下面这样引用它
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.list_example, null);
EditText a = (EditText) v.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(v);
答案 1 :(得分:1)
问题是,当您尝试从视图获取引用时,警报对话框没有视图,因此在将视图设置为AlertDialog之前它没有任何内容,因此它返回NullPointerException,
只需将此代码替换为您的代码
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(view);
alertDialogBuilder
.setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
});
答案 2 :(得分:0)
尝试一下
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
alertDialogBuilder.setView(view);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);
alertDialogBuilder .setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
})
AlertDialog alert = alertDialogBuilder.create();
alert.show();
您应该先扩大视图
答案 3 :(得分:0)
问题是您的警报对话框在从中获取引用时未设置视图,因此在您调用setview之前它没有任何内容,因此您在调用它之前没有提供任何内容,因此您获得了空指针
score, frame = min(zip(scores, frames), key=lambda x: x[0])