我正在尝试从Dialog中获取文本,但它不起作用。除username = txtDialog.getText().toString();
我得到一个NullpointerException
以下是完整的代码:
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = new Dialog(ResActivity.this);
dialog.setContentView(R.layout.custom);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtDialog = (EditText)findViewById(R.id.textDialog);
username = txtDialog.getText().toString();
Intent intent = new Intent(getApplicationContext(), EndActivity.class);
intent.putExtra(MESSAGE_NAME, username);
startActivity(intent);
dialog.dismiss();
}
});
}
});
有人可以帮忙吗?
答案 0 :(得分:2)
试试这个
txtDialog = (EditText)dialog.findViewById(R.id.textDialog);
答案 1 :(得分:2)
我认为Edittext
在Dialog
内
改变这一行
txtDialog = (EditText)findViewById(R.id.textDialog);
到
txtDialog = (EditText)dialog.findViewById(R.id.textDialog);
答案 2 :(得分:0)
你在对话框里面的编辑文本,就像这样使用
txtDialog = (EditText)dialog.findViewById(R.id.textDialog);
答案 3 :(得分:0)
我认为您对话框中的EditText
:
然后更改此声明行
来自
txtDialog = (EditText)findViewById(R.id.textDialog);
要
txtDialog = (EditText)dialog.findViewById(R.id.textDialog);
答案 4 :(得分:0)
void connectToHotSpot(){ AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Inpaint Server");
alert.setMessage("IP Address");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String nickname=input.getText().toString();
if(!nickname.equals(""))
{
Variables.Serversip=nickname;
ipaddress.setText("Server's IP "+nickname);
showAlertMessage("Message","Ok.Proceed");
}
else
{
showAlertMessage("Oops","Specify the Server's IP Address");
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getApplicationContext(),"Must give me the IP B4 Inpainting",Toast.LENGTH_LONG).show();
}
});
alert.show();
}