当前位置:网站首页>[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);
}
边栏推荐
- Differences between = = and = = = in JS (detailed explanation)
- Inter thread synchronization semaphore control
- JS notes
- Traversal and branch judgment of JS (case on June 24, 2022)
- Sword finger offer 09 Implementing queues with two stacks
- 李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE
- Ten routing strategies for distributed task scheduling platform XXL job
- Leetcode topic [array]-34- find the first and last positions of elements in a sorted array
- Continuous integration of aspnetcore & cloud flow
- 免费送书啦!火遍全网的AI给老照片上色,这里有一份详细教程!
猜你喜欢
Geographic location data storage scheme - redis Geo
通俗讲跨域
Educational administration system development (php+mysql)
AutoK3s v0.5.0 发布 延续简约和友好
面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
Startup and shutdown of appium service
Report on Hezhou air32f103cbt6 development board
Create raspberry PI image file of raspberry pie
[golang] leetcode intermediate - find the first and last position of an element in a sorted array & Merge interval
基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022
随机推荐
What is OA
剑指 Offer 10- I. 斐波那契数列
Yadali brick playing game based on deep Q-learning
Desktop development (Tauri) opens the first chapter
说下你对方法区演变过程和内部结构的理解
Asynchronous processing of error prone points
Binocular 3D perception (I): preliminary understanding of binocular
What is the NFT digital collection?
《睡眠公式》:怎么治睡不好?
Sword finger offer 04 Find in 2D array
什么是NFT数字藏品?
Why is it said that restarting can solve 90% of the problems
04. binary tree
Sleep formula: how to cure bad sleep?
不要再「外包」AI 模型了!最新研究发现:有些破坏机器学习模型安全的「后门」无法被检测到
Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
Several relationships of UML
JS notes
Deep learning pytorch cifar10 dataset training "suggestions collection"