[英]How to remove view on the child's parent? android
所以我有一個按鈕,當它被點擊時會顯示一個警報對話框。 我在我的活動的 onCreate 方法中為警報對話框創建視圖。 代碼就在這里:
LayoutInflater factory = LayoutInflater.from(this);
view = factory.inflate(R.layout.grade_result, null);
當我第一次按下按鈕時,對話框以我想要的方式顯示,但是當我第二次按下它時,它會拋出這個異常。
11-28 00:35:58.066:E/AndroidRuntime(30348):由:java.lang.IllegalStateException:指定的孩子已經有一個父母。 您必須首先在孩子的父級上調用 removeView()。
我在按下按鈕時顯示 AlertDialog 的方法的代碼就在這里:
public void details(View v){
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setMessage("Details About Your Grades")
.setCancelable(false)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
alert.show();
任何幫助,將不勝感激! 謝謝!
膨脹應該在 AlertDialog 的構建器中設置的視圖對我有用:
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// inflating view out of the onClick() method does not work
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewGroup viewGroup= (ViewGroup) mInflater.inflate(R.layout.my_view, null);
Dialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle(R.string.my_title)
.setView(viewGroup)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
alertDialog.show();
}
}
我在警報對話框中遇到了類似的問題。 第一次我膨脹了一個視圖,第二次我想顯示一條消息。 如果您顯示警報對話框一次,那么您會膨脹視圖並緩存對話框。 如果您第二次顯示它,您會收到錯誤 IllegalStateException:"call removeView() first on child's parent"
我通過刪除我在第一次單擊時膨脹的視圖並在第二次單擊時顯示消息來解決這個問題。 - 每次在膨脹或創建消息之前刪除視圖或消息。
alertDialogBuilder.setView(null).setMessage(null);
alertDialogBuilder.setView(yourView).setMessage(yourMessage);
我修改了你的代碼,它對我來說很好。
LayoutInflater factory = LayoutInflater.from(this);
mView = factory.inflate(R.layout.grade_result, null);
public void details(View v){
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(null).setMessage(null);
alert.setView(mView);
alert.setMessage("Details About Your Grades")
.setCancelable(false)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
alert.show(); }
用這個 :
final AlertDialog.Builder alert = new AlertDialog.Builder(ProductsListAfterFilter.this);
alert.setTitle("Добавить продукт в корзину?");
alert.setMessage("Введите количество: ");
// Set an EditText view to get user input
final EditText input = new EditText(ProductsListAfterFilter.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int value = 0 ;
try {
value = Integer.valueOf(input.getText().toString());
}
catch (Exception err){
Toast.makeText(getApplicationContext(), "Введите число", 100).show();
}
//insertValue(value,st1,st2,st3,st4);
Toast.makeText(getApplicationContext(), "value = "+value, 100).show();
dialog.dismiss();
dialog.cancel();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
dialog.dismiss();
}
});
final AlertDialog alertd = alert.create();
alertd.show();
它說您的“視圖”已連接到父級。 當您調用 .setView(view) 時會發生這種情況。 如果我是對的 - alertDialog 類為您的視圖提供父級。 這是安卓的基礎。 ViewGroup 可能有很多子視圖。 我有類似的問題。 所以,你可以試試這個 insinde onClick:
ViewGroup vGroup = view.getParent();
vGroup.removeView(view);
// And right after that - close dialog
dialog.dismiss();
對我有幫助,希望對你有幫助!
使用 alert.create(); 然后alertd.dismiss(); 銷毀對話框
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final AlertDialog alertd = alert.create();
alert.setTitle("Title");
alert.setMessage("Messaage");
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
alertd.dismiss();
}
});
使用此代碼
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setMessage("Details About Your Grades")
.setCancelable(false)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
}
});
AlertDialog alertdialog = alert.create();
alertdialog .show()
您可以在對話框中設置視圖之前將視圖設置為空。
像這樣:
alert.setView(null);
alert.setView(view);
在再次設置之前,它將始終將視圖設置為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.