简体   繁体   English

对话框和edittext侦听器:setEnabled崩溃

[英]dialog and edittext listener: crash at setEnabled

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. 我正在尝试在edittext内创建一个对话框。如果edittext为空,则必须禁用肯定button否则必须启用。 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); 当我执行此代码并在edittext编写一些内容时,如果在此行的if内的else处得到NullPointerException button.setEnabled(false); Where is the problem? 问题出在哪儿?

The scope of if and else is only upto next statment. if和else的范围仅取决于下一个语句。 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);
}

@Happy_New_Year is right. @Happy_New_Year是正确的。 You are missing {} in else parts. 您在其他部分缺少{}。 If you don't put {}, then the only very next statement would be considered as the else part. 如果您不输入{},则仅下一个语句将被视为else部分。 The button.setEnabled(false); button.setEnabled(false); is outside of else block. 在else块之外。 So the button object is not being initialized here. 因此,此处未初始化button对象。

Problem is at text = mEditText.getText().toString(); 问题出在text = mEditText.getText().toString();

when text is null null.toString(); 当文本为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);

}

The error is obviously in this line :- 错误显然在此行中:-

        mEditText = new EditText(mContext);

By doing this, you have created a new object for an Edittext. 这样,您为Edittext创建了一个新对象。 We do such thing when we create a dynamic edittext or any other widget. 当我们创建动态edittext或任何其他小部件时,我们会执行此类操作。

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的引用,该引用必须在该edittext的xml文件中的“ id”属性中给出,例如:

 <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); 在您的课程中,您首先需要这样做: mEditText = (EditText)findViewById(R.id.editText1);

I hope you got it now 我希望你现在明白了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM