问题
Hello I am going crazy to understand where is the problem in this dialog. I am trying to create one dialog with inside an edittext.. If the edittext is empty the positive button must be disabled else enabled. I wrote this code.
public class Example extends AlertDialog {
AlertDialog.Builder builder;
EditText mEditText;
Context mContext;
Button button;
String text;
protected Example(Context context) {
super(context);
// TODO Auto-generated constructor stub
builder = new AlertDialog.Builder(context);
this.mContext = context;
mEditText = new EditText(mContext);
builder.setView(mEditText);
builder.setPositiveButton("Okay", null);
builder.setNegativeButton("No", null);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
AlertDialog dialog = builder.create();
text = mEditText.getText().toString();
if(text.trim().length()>0) {
button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
if(button != null)
button.setEnabled(true);
else
button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
}
else
button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
builder.setTitle("Example Dialog");
builder.create();
builder.show();
}
}
When i execute this code and write something in edittext i get NullPointerException at the else inside the if at this line button.setEnabled(false); Where is the problem?
回答1:
The scope of if and else is only upto next statment. If you want to use more than one statement than enclose it in block. For example-
else
{
button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
}
回答2:
@Happy_New_Year is right. You are missing {} in else parts. If you don't put {}, then the only very next statement would be considered as the else part. The button.setEnabled(false); is outside of else block. So the button object is not being initialized here.
回答3:
Problem is at text = mEditText.getText().toString();
when text is null null.toString(); will crash.
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null)
{
if (s.length() > 0)
{
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
}
else {
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}
}
}
Here is entire code that I used in my project
AlertDialog.Builder builder; // Declare Globally
AlertDialog dialog; // Declare Globally
builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Add New Keyword");
final EditText input = new EditText(this);
input.setHint("Min 3 Chars");
builder.setView(input);
input.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null)
{
if (s.length() > 2)
{
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
}
else {
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (input != null)
{
System.out.println("Entered Text" + input.getText().toString().trim());
}
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}
回答4:
The error is obviously in this line :-
mEditText = new EditText(mContext);
By doing this, you have created a new object for an Edittext. We do such thing when we create a dynamic edittext or any other widget.
In a normal scenario, you need to pass a reference to your edittext which you must have given in a property called as "id" in an xml file for the edittext for example :-
<EditText
android:id = "@+id/editText1"
<!-- other properties-->
</EditText
and in your class, you need to do like this firstly :- mEditText = (EditText)findViewById(R.id.editText1);
I hope you got it now
来源:https://stackoverflow.com/questions/20867021/dialog-and-edittext-listener-crash-at-setenabled