android中EditText为空时如何禁用按钮
如果我的msgtext(editext)为空,我想禁用发送按钮 我有一个对话框,其中有edittext和send按钮 当对话框打开时 并检查edittext是否为空,然后我的按钮处于禁用状态 这是我的密码android中EditText为空时如何禁用按钮,android,nullpointerexception,dialog,Android,Nullpointerexception,Dialog,如果我的msgtext(editext)为空,我想禁用发送按钮 我有一个对话框,其中有edittext和send按钮 当对话框打开时 并检查edittext是否为空,然后我的按钮处于禁用状态 这是我的密码 public void openDialog(){ dialog = new Dialog(CommentsActivity.this); dialog.requestWindowFeature(Window.FEATURE_NO_TIT
public void openDialog(){
dialog = new Dialog(CommentsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialogcommentlayout);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
dialog.getWindow().setGravity(Gravity.FILL);
dialog.setCanceledOnTouchOutside(false);
msgtext=(EditText)dialog.findViewById(R.id.et_sent_msg);
msgtext.addTextChangedListener(textWatcher);
checkFieldsForEmptyValues();
//buttton for send comment
send=(Button)dialog.findViewById(R.id.sent_msg);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
new sendReply().execute();
}
});
mHlvSimpleList= (ListView) dialog.findViewById(R.id.feedlist);
mHlvSimpleList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(CommentsActivity.this, "Listview", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
//asynktask to show feed comment in dialog
}
private void checkFieldsForEmptyValues(){
String s1 = msgtext.getText().toString().trim();
if(s1.equals(""))
{
send.setEnabled(false);
}
}
给出错误空指针验证
在
send.setEnabled(false);这条线
请告诉我哪里做错了什么?
thanku当您执行
send.setEnabled(false)时,您的send
按钮仍然为空代码>,在检查字段fremptyvalues()
中。您必须首先初始化它,然后执行该方法,这样它就不会为null。像这样:
//buttton for send comment
send=(Button)dialog.findViewById(R.id.sent_msg);
checkFieldsForEmptyValues();
这将解决您的NullPointerException
问题
//buttton for send comment
send=(Button)dialog.findViewById(R.id.sent_msg);
checkFieldsForEmptyValues();