【问题标题】:alertDialog.getButton() method gives null pointer exception androidalertDialog.getButton() 方法给出空指针异常android
【发布时间】:2011-06-03 23:48:55
【问题描述】:

我打算用 layout_weight=1 创建 3 个按钮,对自定义对话框不感兴趣。所以我写了下面的代码。它不起作用。总是是按钮给我 null。 这段代码有什么问题?

  AlertDialog dialog= new AlertDialog.Builder(this).create();
            dialog.setIcon(R.drawable.alert_icon);
            dialog.setTitle("title");
            dialog.setMessage("Message");
            dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                                                }
            });
            Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            Log.w("Button",""+yesButton);//here getting null
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);
            yesButton.setLayoutParams(layoutParams);
            dialog.show();

问候, Android 开发者。

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    在这里查看答案:http://code.google.com/p/android/issues/detail?id=6360

    正如评论#4 中所说,您必须先在对话框中调用show(),然后才能访问这些按钮,它们事先不可用。有关如何在按钮准备好后立即修改按钮的自动解决方案,请参阅Mickeys answer

    【讨论】:

    • 问题仍然存在。不使用该链接。
    • 请阅读评论#4,使用 dialog.show();在使用 getButton() 之前
    【解决方案2】:

    这对我有用:

    AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setMessage(message)
                    .setCancelable(true)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                //do smthng
                            })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //do snthn
                        }
                    }).create();
    
            alertDialog.setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {                    //
                    Button positiveButton = ((AlertDialog) dialog)
                            .getButton(AlertDialog.BUTTON_POSITIVE);
                    positiveButton.setBackgroundDrawable(getResources()
                            .getDrawable(R.drawable.btn_default_holo_dark));
    
                    Button negativeButton = ((AlertDialog) dialog)
                            .getButton(AlertDialog.BUTTON_NEGATIVE);
                    positiveButton.setBackgroundDrawable(getResources()
                            .getDrawable(R.drawable.btn_default_holo_dark));
                }
            });
    
            alertDialog.show(); 
    

    仅按此顺序,在create() 之后调用alertDialog.setOnShowListener

    【讨论】:

    • setOnShowListener 是 API 8+
    【解决方案3】:

    谢谢你。但是对于新开发人员的理解目的,我正在重写下面的代码

    AlertDialog dialog= new AlertDialog.Builder(this).create();             
    dialog.setIcon(R.drawable.alert_icon);             
    dialog.setTitle("title");            
    dialog.setMessage("Message");             
    dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {                 
        @Override                 
        public void onClick(DialogInterface arg0, int arg1) {                                                
        }             
    }); 
    dialog.show(); 
    Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);             
    Log.w("Button",""+yesButton); //here getting null             
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);             
    yesButton.setLayoutParams(layoutParams);        
    

    【讨论】:

      【解决方案4】:

      以下是有用的 Kotlin 扩展功能,可通过自动解决方案实现您想要的功能:

      import android.content.DialogInterface.BUTTON_NEGATIVE
      import android.content.DialogInterface.BUTTON_POSITIVE
      import androidx.appcompat.app.AlertDialog
      
      fun AlertDialog.onPositiveButtonClick(action: () -> Unit) {
          onButtonClick(BUTTON_POSITIVE, action)
      }
      
      fun AlertDialog.onNegativeButtonClick(action: () -> Unit) {
          onButtonClick(BUTTON_NEGATIVE, action)
      }
      
      fun AlertDialog.onButtonClick(whichButton: Int, action: () -> Unit) {
          fun onButtonClick() = getButton(whichButton).setOnClickListener { action() }
      
          if (isShowing) {
              onButtonClick()
          } else {
              setOnShowListener {
                  onButtonClick()
              }
          }
      }
      

      如何使用:

      alertDialog.onPositiveButtonClick { /* DO WHAT YOU WANT HERE */ }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 2012-12-10
        • 2014-09-06
        • 2014-07-19
        • 2017-01-05
        • 1970-01-01
        相关资源
        最近更新 更多