[英]Android Input Dialog Box's Onclick doesn't work
so I am new to programming in Android so I apologize if this is a beginner mistake, but I was programming my button to add a user to an array list with the name given in the dialog box. 因此,我是Android编程的新手,因此如果这是一个初学者的错误,我深表歉意,但是我正在对按钮进行编程,以将用户添加到对话框中给定名称的数组列表中。 When I run it, everything works except when it executes users.add(new User(name)); 当我运行它时,除了执行users.add(new User(name));之外,其他所有东西都可以工作。 (It returns a null pointerI'm unsure as to why this is the case. Any help would be greatly appreciated. Here is my code. (它返回一个空指针,我不确定为什么会这样。任何帮助将不胜感激。这是我的代码。
addPersonButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final EditText input = new EditText(mainActivity);
new AlertDialog.Builder(mainActivity)
.setTitle("New User")
.setMessage("What is the new user's name?")
.setView(input)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
final String name = input.getText().toString();
users.add(new User(name));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
}
});
i think your problem is with input
in onClick
method, and because input
in your dialog layout
not in main layout
, so you must use following code: 我觉得你的问题是与input
中onClick
方法,并因为input
您的dialog layout
不是main layout
,所以你必须使用下面的代码:
final String name = dialog.input.getText().toString();
users.add(new User(name));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.