使用一个对话框提示的android用户的输入对话框、提示、用户、android

2023-09-04 03:52:47 作者:余生都是你

我想提示用户使用一个对话框,让我输入我的Andr​​oid应用程序。这是我发现:

I would like to prompt the user to give me input in my android application using a dialog. this is what I have found:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
 // Do something with value!
 }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int whichButton) {
     // Canceled.
}
});

 alert.show();

但这 给我:

but this gives me :

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

有我的$ C $的任何问题,C中,它似乎是一个空参数传递对话框上,但我无法找出是什么问题。

is there any problem on my code it seems like a null argument is passed on the dialog but I can't find out what is the problem.

推荐答案

当我跑到你的code在一个新的项目,它工作得很好。所以,可能是这个您正在使用

When I ran your code in a new project, it worked fine. So probably "this" that you are using

是不是一个活动 是不考虑的活性​​,即有可能是一个父活动。如果是一些活动的儿童,使用,而不是本。的getParent() 为null is not an activity is not the activity in view i.e. there might be a parent activity. If it is the child of some activity, use getParent() instead of "this". is null

希望这有助于。