当前位置:网站首页>[problem solving] dialogfragment can not be attached to a container view
[problem solving] dialogfragment can not be attached to a container view
2022-06-25 15:59:00 【Favorite grapes】
DialogFragment can not be attached to a container view
The whole story of the problem
In a DialogFragment in , Shows another Dialog, the other one Dialog Display time , Click OK to delete the current Dialog Show again
Pseudo code structure
class MyDialogFragment extends DiabogFragment {
View contentView;
FragmentActivity activity;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (contentView == null) {
contentView = inflater.inflate(getLayoutId(), container, false);
}
return contentView;
}
......
public void onBtnClick() {
DialogUtils.showMsgDialog("Msg", new OnOkBtnClick(){
public void onClick(Dialog dialog) {
DialogUitls.showDialogFragment(MyDialogFragment.this, activity);
dialog.dismiss();
}
});
dismiss();
}
}
problem
There is no problem with the first display , But when it shows MsgDialog, Click on Button Show again Dialog when , newspaper DialogFragment can not be attached to a container view.
reason
if (contentView == null) {
contentView = inflater.inflate(getLayoutId(), container, false);
}
Show again Dialog when , No view is recreated , and DialogFragmnet stay onActivityCreated Judge whether the view has Parent. And because the original contentView The object has not been destroyed , Through it getParent Is not for null, So this exception is thrown .
Solution 1
take contentView Put it in a local variable .
Solution 2.
// Call the parent method first ( Because constant return is empty , There will be no problem )
contentView = super.onCreateView(inflater, container, savedInstanceState);
if (contentView == null) {
contentView = inflater.inflate(getLayoutId(), container, false);
}
边栏推荐
- Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
- Jz-065 path in matrix
- Binocular 3D perception (I): preliminary understanding of binocular
- Do you want to go to an outsourcing company? This article will give you a comprehensive understanding of outsourcing pits!
- JS中的==和===的区别(详解)
- Resolve Visio and office365 installation compatibility issues
- f_read 函数[通俗易懂]
- JS的注释
- Report on Hezhou air32f103cbt6 development board
- Sword finger offer 10- I. Fibonacci sequence
猜你喜欢

Sword finger offer 07 Rebuild binary tree

Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected

Linux-MySQL数据库之高级SQL 语句一
Open the box to experience rust, come on!!!

Don't underestimate the integral mall, its role can be great!

Asynchronous processing of error prone points
MySQL installation tutorial

不要小看了积分商城,它的作用可以很大!

Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE

商城风格也可以很多变,DIY了解一下!
随机推荐
Several relationships of UML
NFT元宇宙发展能做什么?
leetcode-8. String to integer (ATOI)
Mixed density network (MDN) for multiple regression explanation and code example
MySQL installation tutorial
Educational administration system development (php+mysql)
QC, QA, IPQC, JQE, DQA, SQE, DQC, MQC, IQC, FQC, OQC
Power representation in go language
golang正则regexp包使用-05-扩展Expand()、根据正则切割Split()
Client development (electron) system level API usage 2
剑指 Offer 07. 重建二叉树
Error com mysql. cj. jdbc. exceptions. Communicationsexception: solutions to communications link failure
Highly concurrent optimized Lua + openresty+redis +mysql (multi-level cache implementation) + current limit +canal synchronization solution
Finally, we can figure out whether the binding event in the tag is bracketed or not
Analysis of the concept of metacosmic system
What is session? How is it different from cookies?
JS notes
免费送书啦!火遍全网的AI给老照片上色,这里有一份详细教程!
Time wheel and implementation analysis of time wheel in go zero
加载本地cifar10 数据集