2012-02-11 47 views
18

我有一個類來創建對話框和編碼以從中獲取值。它適用於一個。當我嘗試第二次調用對話框時,它傳遞以下錯誤消息。指定的孩子已經有父母。您必須首先調用子視圖的父級的removeView()

:java.lang.IllegalStateException:指定的子項已具有父項。您必須先調用子對象的父對象的removeView()。

你能告訴我如何刪除removeView()嗎?

這裏是類的代碼;

package com.util; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.widget.EditText; 

/** 
* helper for Prompt-Dialog creation 
*/ 
public abstract class PromptDialog extends AlertDialog.Builder implements OnClickListener { 
private final EditText input; 

/** 
    * @param context 
    * @param title resource id 
    * @param message resource id 
    */ 
public PromptDialog(Context context, int title, int message) { 
    super(context); 
    setTitle(title); 
    //:TODO Display msg only if not empty 
    //setMessage(message); 

    input = new EditText(context); 
    setView(input); 

    setPositiveButton("ok", this); 
    setNegativeButton("cancel", this); 
} 

/** 
    * will be called when "cancel" pressed. 
    * closes the dialog. 
    * can be overridden. 
    * @param dialog 
    */ 
public void onCancelClicked(DialogInterface dialog) { 
    dialog.dismiss(); 
} 

@Override 
public void onClick(DialogInterface dialog, int which) { 
    if (which == DialogInterface.BUTTON_POSITIVE) { 
    if (onOkClicked(input.getText().toString())) { 
    dialog.dismiss(); 
    } 
    } else { 
    onCancelClicked(dialog); 
    } 
} 

/** 

     * called when "ok" pressed. 
     * @param input 
     * @return true, if the dialog should be closed. false, if not. 
     */ 
    abstract public boolean onOkClicked(String input); 
    } 

這裏是我調用類的實例的代碼;

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 



final PromptDialog dlgName = new PromptDialog(this, R.string.enterName, R.string.enter_comment) { 
      @Override 
      public boolean onOkClicked(String input) { 
       // do something 
       mName = input; 
        save(); 
          //end do some thing 
       return true; // true = close dialog 
      } 
     };  


    mTxtShiftName = (TextView) findViewById(R.id.shiftname); 
      mTxtShiftName.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       dlgName.show(); 
      } 
     }); 
+0

你在第二次嘗試時再次調用對話框構造函數嗎? – 2012-02-12 00:01:56

+0

我使用的所有編碼都在我的問題中複製。我認爲這可能是原因。但我不知道如何避免這種情況? – SAN 2012-02-12 00:22:21

+0

單擊按鈕時不要調用構造函數兩次。在onCreate中使用Dialog構造函數或onPrepareDailog使用代碼創建對話框,然後在想要顯示時調用dialog.show()。 – 2012-02-12 00:25:22

回答

1

你應該把代碼調用onCreateDialog(int)回調方法,而不是onCreate(Bundle)內部對話框構造。在您的代碼中,當您撥打dlgName.show()時,該對話框將被隱式初始化。因此,當您第二次調用對話框時,對話框構造函數就是如此。

66

我從我的片段的onCreateView()調用中調用錯誤的膨脹方法得到此錯誤。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_saves, container); 
} 

向該:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_saves, container, false); 
} 
+1

花花公子作爲最後一個參數拯救了我的生命!謝謝! – 2014-07-25 00:16:11

+0

給那個男人一枚獎章!謝啦 – Karoly 2016-03-20 22:48:05

0

檢查了這一點:

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int,android.view.ViewGroup,boolean)

具體而言,布爾參數和返回值

我通過從該改變固定它在LayoutInflator的膨脹方法中:

返回 充氣層次結構的根視圖。 如果提供了root並且attachToRoot爲true,則這是根; 否則它是膨脹的XML文件的根。

View dialogView = inflater.inflate(R.layout.brush_opts_dialog, rootView, false); 

你想使充氣視圖的根是創建的視圖,而不是「本」,這將是一個活動的整個內部片段。

相關問題