2014-07-18 67 views
0

我想禁用对话框上的正面按钮,但我无法。这里是我的对话框:禁用BUTTON_POSITIVE抛出空指针

AlertDialog dialog = new AlertDialog.Builder(this) 
     .setTitle(R.string.delete_db_on_exit) 
     .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null)) 
     .setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       _application.setDeleteDatabaseOnExit(); 
       dialog.cancel(); 
       //Navigate back to main activity 
      } 
     }) 
     .create(); 

dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false); 
dialog.show(); 

即使我只是设定清晰的按键,当我引用它,我得到一个空指针异常。这是因为我在哪里引用它? Android文档中没有提到的Android对话框是否有重大变化?

我注意到我无法做到他们在当前的documentation中注意到的任何事情。

回答

0

我的理解是你的问题是 :: 你试图取消对话框中的正面按钮对话框onclick。


而不是 ::

AlertDialog dialog = new AlertDialog.Builder(this) 
      .setTitle(R.string.delete_db_on_exit) 
      .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null)) 
      .setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        _application.setDeleteDatabaseOnExit(); 
        dialog.cancel(); 
        //Navigate back to main activity 
       } 
      }) 
      .create(); 

    dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false); 
    dialog.show(); 

试试这个 ::

AlertDialog dialog = new AlertDialog.Builder(this) 
      .setTitle(R.string.delete_db_on_exit) 
      .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null)) 
      .setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }) 
      .create(); 

    dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false); 
    dialog.show(); 

也将你的代码的对话框调用之后....

_application.setDeleteDatabaseOnExit(); 
//Navigate back to main activity 

我认为你不能把它放在对话框按钮动作中(如果可能的话使用标志)希望它能帮助!

+0

嘿Devrath:

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); dialog.show(); 

到了!我的问题是'dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false);'会抛出一个空指针异常,因为没有'BUTTON_POSITIVE'。 –

0

我需要调用getButton(AlertDialog.BUTTON_POSITIVE)dialog.show()

FROM:

dialog.show(); 
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);