首页 > 代码库 > 解决alaert.builder二次调用报错的bug

解决alaert.builder二次调用报错的bug

报错的代码是:

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

大致是说你的弹出框中的view已经绑定了一个paren,就是你的弹出框。当你再次调用显示的时候,他以为你又要绑定到一个新的窗口上去。解决的方法是每次显示之前都重新填充设置view.如下:每次点击的时候调用:

public void startClick(View view){
		LayoutInflater inflater = LayoutInflater.from(getApplication()); 
		 mFloatLayout = (LinearLayout)inflater.inflate(R.layout.province_item, null);
		 builder =  new AlertDialog.Builder(MainActivity.this)
	        .setView(mFloatLayout);
		ad = builder.show();
	}