当前位置:网站首页>[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);
}
边栏推荐
- sql优化的几种方式
- Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
- Introduction to MgO 256gb NAND flash chip
- Programmer vs hacker thinking | daily anecdotes
- 商城风格也可以很多变,DIY了解一下!
- What is session? How is it different from cookies?
- 面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
- Most commonly used SQL statements
- Constructor Pattern
- Practice of geospatial data in Nepal graph
猜你喜欢

TFIDF与BM25

Source code analysis of nine routing strategies for distributed task scheduling platform XXL job

LeCun预言AGI:大模型和强化学习都是斜道!我的「世界模型」才是新路

For the first time in Chinese universities! Unique in the world! Tongji students win international awards

Continuous integration of aspnetcore & cloud flow

合宙Air32F103CBT6開發板上手報告

读配置、讲原理、看面试真题,我只能帮你到这了。。。
Inter thread synchronization semaphore control

免费送书啦!火遍全网的AI给老照片上色,这里有一份详细教程!

剑指 Offer 10- I. 斐波那契数列
随机推荐
Pytest测试框架笔记
地理位置数据存储方案——Redis GEO
Sword finger offer 09 Implementing queues with two stacks
不要小看了积分商城,它的作用可以很大!
[issue 24] one year experience of golang to develop futu
Leetcode topic [array]-34- find the first and last positions of elements in a sorted array
分享自己平时使用的socket多客户端通信的代码技术点和软件使用
TFIDF与BM25
TFIDF and BM25
The style of the mall can also change a lot. DIY can learn about it!
The paid video at station B caused the up master to lose more than ten thousand fans
Introduction to MgO 256gb NAND flash chip
Golang regular regexp package uses -05- extend expand(), cut split() according to the rule
04. binary tree
What is OA
What are the reasons why the game industry needs high defense servers?
JS中的==和===的区别(详解)
Resolve Visio and office365 installation compatibility issues
The release of autok3s v0.5.0 continues to be simple and friendly
Yadali brick playing game based on deep Q-learning