Android:alertDialog不工作

Android:alertDialog不工作,android,android-alertdialog,Android,Android Alertdialog,我试图在点击按钮时添加一个警告对话框,但有些人认为它不起作用。。但我加的祝酒词效果很好。谁能帮帮我吗。在创建对象[new AlertDialog.Builder(context.create();]时,我直接添加了上下文而不是“this”,因为在添加此对象时,它给了我一个错误“构造函数AlertDialog.Builder(new View.OnClickListener(){})未定义” 在设置任何字段之前,在调用create()之前,调用alertDialog上的show()方法,并且不调用

我试图在点击按钮时添加一个警告对话框,但有些人认为它不起作用。。但我加的祝酒词效果很好。谁能帮帮我吗。在创建对象[new AlertDialog.Builder(context.create();]时,我直接添加了上下文而不是“this”,因为在添加此对象时,它给了我一个错误“构造函数AlertDialog.Builder(new View.OnClickListener(){})未定义”

在设置任何字段之前,在调用create()之前,调用
alertDialog
上的
show()
方法,并且不调用show()

android文档指出,最好在活动的onCreateDialog(int)回调方法中定义对话框的创建

您可以这样做,然后通过使用相关Id调用showDialog(int)在按钮onClickListener中显示对话框

       public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Service service = (Service) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        Drawable d = convertView.findViewById(R.id.submit).getBackground();  
        PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);  
        d.setColorFilter(filter); 
        tv.setText("   " + service.getName());
        this.submitButton = (Button)convertView.findViewById(R.id.submit);
        this.submitButton.setText("Activate");
        this.submitButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                Toast.makeText(context, "Service Activation Request Send", Toast.LENGTH_LONG).show();
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("Alert 1");
                alertDialog.setMessage("This is an alert");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    return;
                } }); 
                }
              });

        //this.submitButton.setPadding(20, 0, 0, 0);

        // Depending upon the child type, set the imageTextView01
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        if (service instanceof DataService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.data, 0, 0, 0);
        } else if (service instanceof VoiceService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.voice, 0, 0, 0);
        } else if (service instanceof SmsService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms, 0, 0, 0);
        }
        return convertView;
        }