The specified child already has a parent. You must call removeView() on the child‘s parent first.
程序员文章站
2024-01-17 19:42:40
...
使用自定义对话框AlertDialog出现The specified child already has a parent. You must call removeView() on the child’s parent first.
分析:
是因为布局被重复加载。
解决办法:
如果已经有了父容器,就不进行加载
如果没有父容器,就进行加
private AlertDialog.Builder builder;
private AlertDialog dialog;
private View view;
open_alertdialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//一定要判断view是否已经被加载
if (view.getParent() == null) {
//加载布局
builder.setView(view);
dialog = builder.create();
}
//显示布局
dialog.show();
}
});
在onCreate方法中直接进行初始化了
//创建对话窗
builder = new AlertDialog.Builder(MainActivity.this);
//设置对话框的布局
view = View.inflate(MainActivity.this, R.layout.alertdialog_mine, null);
open_photo = view.findViewById(R.id.open_photo);
open_album = view.findViewById(R.id.open_album);
我就不想详细写了,如果有问题,请留言询问,我会尽快回复的。
上一篇: The specified child already has a parent. You must call removeView() on the child's parent first.
下一篇: 训练Word2Vec报错:RuntimeError: you must first build vocabulary before training the model
推荐阅读
-
The specified child already has a parent. You must call removeView() on the child‘s parent first.
-
The specified child already has a parent. You must call removeView() on the child's parent first.
-
The specified child already has a parent. You must call removeView() on the child's parent first,
-
The specified child already has a parent. You must call removeView() on the child's parent first