You must call removeView() on the child's parent first


在做alertdialog是的時候報了這么一個錯誤:

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

搞了許久,終於理解了。

et1 = (EditText)findViewById(R.id.editText1);
builder.setView(et1); -- AlertDialog.Builder builder

et1我寫在了xml里面,這樣報錯,原因是一女不可二嫁。

et1的parent即是R.layout.main 又是AlertDialog。

自然就報錯了要你removeView()了。

解決方法有兩種

1.動態生成EditText

et1 = new EditText(this);
builder.setView(et1);

2. 放在另一個xml中,用inflater

LayoutInflater inflater = LayoutInflater.from(this);  
View textEntryView = inflater.inflate(R.layout.test1, null);
et1 = (EditText)textEntryView.findViewById(R.id.editText1);
builder.setView(textEntryView ); 注意這里是textEntryView ,不是et1 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM