AlertDialog getButton()方法返回null

时间:2013-07-13 01:52:33

标签: android alertdialog android-alertdialog

我已经设置了AlertDialog这样:

AlertDialog.Builder noteAlert = new AlertDialog.Builder(ClassName.this);
noteAlert.setTitle("Title");
noteAlert.setMessage("Message");
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});
noteAlert.setNeutralButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});
noteAlert.setNegativeButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});

AlertDialog alertDialog = noteAlert.create();                                   
Button deleteButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
if (someCondition != 1)
    // code runs till here
    deleteButton.setEnabled(false); // code works on deleting this line

noteAlert.show();

当我运行上面的代码时,它会一直运行到if语句。然后应用程序崩溃(我假设getButton()抛出一个NPE)。我在SO上看到了许多其他答案,它们提供的代码与禁用按钮的解决方案相同。

当我注释掉setEnabled()行时,应用程序正常工作(只有按钮不会被禁用)。所以基本上我试图禁用这个NegativeButton并且它不起作用。你们能提出一些解决方案吗?

的logcat:

  

07-13 08:01:14.378:D / ViewRootImpl(19779):ViewRoot TouchDown(Absolute)DOWN(380,691)

     

07-13 08:01:14.495:E / dialog(19779):AlertDiablog开始

     

07-13 08:01:14.495:E / hasnote(19779):0

     

07-13 08:01:14.511:E / hasnote(19779):0

     

07-13 08:01:14.511:D / AndroidRuntime(19779):关闭虚拟机

     

07-13 08:01:14.511:W / dalvikvm(19779):threadid = 1:线程退出未捕获的异常   (组= 0x40e392a0)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):致命异常:主

     

07-13 08:01:14.519:E / AndroidRuntime(19779):java.lang.NullPointerException

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   com.example.sherlockcaldroid2.TestSubjectCalendar $ 1 $ 2.onClick(TestSubjectCalendar.java:250)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:1   66)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   android.os.Handler.dispatchMessage(Handler.java:99)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):在android.os.Looper.loop(Looper.java:137)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   android.app.ActivityThread.main(ActivityThread.java:4849)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   java.lang.reflect.Method.invokeNative(Native Method)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   java.lang.reflect.Method.invoke(Method.java:511)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)

     

07-13 08:01:14.519:E / AndroidRuntime(19779):at dalvik.system.NativeStart.main(Native   方法)

     

07-13 08:01:34.089:I / Process(19779):发送信号。 PID:19779 SIG:9

4 个答案:

答案 0 :(得分:1)

我很确定视图在生命周期的后期(即在调用show()时)不会膨胀。

我快速查看了文档并找不到build()或inflate()方法,但我希望最简单的方法就是在按钮操作逻辑之前移动noteAlert.show()

编辑: 你有没有尝试过改变:

noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});

noteAlert.setButton(DialogInterface.BUTTON_POSITIVE, "Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});

答案 1 :(得分:1)

这是因为在通话deleteButton.setEnabled(false)时按钮不存在。您应该在noteAlert.show()之前移动电话deleteButton.setEnabled(false)

答案 2 :(得分:0)

试试这个解决方案:

AlertDialog.Builder noteAlert = new AlertDialog.Builder(ClassName.this); 
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});
noteAlert.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if(**some condition**)
                {
                    Button button = builder.getButton(AlertDialog.BUTTON_POSITIVE);
                    if (button != null) {
                        button.setEnabled(false);
                    }
                }
            }
        });

答案 3 :(得分:0)

dialog.show之后访问按钮。否则将是null

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
                builder.setTitle("Title");
                builder.setMessage("message");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Do Something
                    }
                });
 builder.setNegativeButton(context.getResources().getString(R.string.no_text), (dialog, which) -> {
                            dialog.cancel();
                            listener.onNoButtonClick();
                        });
                AlertDialog dialog = builder.create();
                dialog.show();
               dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);