AlertDialog.Builder没有被正确删除

时间:2012-01-10 10:54:11

标签: android dialog alertdialog

继续我从另一个问题开始,我已经创建了一个新类来建立一个AlertDialog.Builder。这用于我的应用程序的多个类。我在我正在使用的类中创建类的对象,然后在需要时调用show()方法。这看起来是正确的。

我遇到麻烦的是,一旦我再次调用show(),我就会收到以下错误:

    java.lang.IllegalStateException: The specified child already has a parent. 
You must call removeView() on the child's parent first

我不是100%确定我需要调用此removeView或specificc视图。以下是Dialog Class的代码

public class CommentDialog {
    private final Context context;
    private final AlertDialog.Builder builder;    
    private String text;

    private EditText editText;

    public CommentDialog(Context context) {
        this.context = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.comment_dialog, null);

        editText = (EditText) layout.findViewById(R.id.comment_text);
        editText.setText(getCommentText());

        builder = new AlertDialog.Builder(context);

        builder.setView(LayoutInflater.from(context).inflate(R.layout.round_comment_dialog, null));
        builder.setTitle(R.string.round_comment_title); 
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {

                               setCommentText(editText.getText().toString());

                           }
                        });
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {


                           }
                        });
        builder.create();
    }

    public void setCommentText(String pCommentText) {
        text = pCommentText;
    }

    public String getCommentText() {
        return text;
    }

    public void show() {
        builder.show();
    }


}

感谢你的时间

EDITTED

添加了round_comment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dip"
    android:background="@drawable/border_main"
    android:orientation="vertical" >

        <EditText
            android:id="@+id/comment_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:gravity="top|left"
            android:inputType="textMultiLine"
            android:lines="5" >
        </EditText>

</LinearLayout>

EDITTED MK2

在主要课程中,我正在做以下事情:

以上构造函数

private CommentDialog myDialog;

在oncreate()

myDialog = new CommentDialog(this);

然后按下按钮:

case R.id.round_comment_button:
            myDialog.setommentText(comentText);
            myDialog.show();

            roundComentText = myDialog.getRoundCommentText();

            break;

4 个答案:

答案 0 :(得分:1)

最好让你的类扩展Dialog类并构建它然后显示它。当然,您的视图将在Dialog类的构造函数中创建。由于Dialog没有可用的Positive和Negative按钮,因此您必须创建自己的按钮并为Dialog设置匿名侦听器以收听它们。其他一切都应该很容易。

答案 1 :(得分:0)

public CommentDialog(Context context) 
{
        this.context = context;
}


**Replace the constructor with Activity**

public CommentDialog(Activity activity) 
{
        this.activity= activity;
}

答案 2 :(得分:0)

错误是自我解释,您正在设置的“内容视图/布局”的视图已经具有其布局。
您能否从日志中拉出发生此错误的行。
然后尝试查看您是否在该代码中将任何布局/视图设置为代码中的任何视图。
如果是这样,则在设置其内容视图/布局之前调用其removeView()。

答案 3 :(得分:-2)

试试这个

 protected Dialog onCreateDialog(int id) {
 switch (id) {
 case 0:
 return new AlertDialog.Builder(this)
         .setIcon(R.drawable.icon)
         .setTitle(“This is a dialog with some simple text...”)
         .setPositiveButton(“OK”, new
    DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog,
    int whichButton)
 {
  Toast.makeText(getBaseContext(),
  “OK clicked!”, Toast.LENGTH_SHORT).show();
  }
 })
       .setNegativeButton(“Cancel”, new
        DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
       int whichButton)
     {
       Toast.makeText(getBaseContext(),
      “Cancel clicked!”, Toast.LENGTH_SHORT).show();
  }
  })