Android:alertDialog无法正常工作

时间:2011-08-08 12:42:54

标签: android alertdialog

我正在尝试添加单击按钮的警报对话框,但有些它是如何工作的...但我添加的吐司工作完美。有人可以帮帮我。在创建对象时,我直接添加了上下文而不是“this”[new AlertDialog.Builder(context).create(); ]当我添加这个时,它给了我错误“构造函数AlertDialog.Builder(new View.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;
        }

2 个答案:

答案 0 :(得分:3)

show()上调用alertDialog方法。

答案 1 :(得分:0)

您在设置任何字段之前调用create(),而且您没有调用show()

android文档声明最好在我们的Activity的onCreateDialog(int)回调方法中定义对话框的创建

http://developer.android.com/guide/topics/ui/dialogs.html

你可以这样做,然后通过调用showDialog(int)和相关的Id

在onClickListener按钮中显示你的对话框