在片段的onCreateview()上显示警报对话框
我是Android开发的新手。从我的Activity.java文件中,我正在检查Create_task菜单项是否单击。如果单击,我想显示一个弹出屏幕create_task(xml中的布局)。
activity.java
case R.id.create_task:
CreateTaskFragment createTaskFragment = new CreateTaskFragment();
FragmentTransaction createTaskft = getSupportFragmentManager().beginTransaction();
createTaskft.replace(R.id.flContent, createTaskFragment);
createTaskft.commit();
createTaskFragment.java
public class CreateTaskFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.create_task, container, false);
AlertDialog newTaskdialog = new AlertDialog.Builder(getContext())
.setView(view)
.create();
newTaskdialog.show();
newTaskdialog.setCanceledOnTouchOutside(true);
return view;
}
}
我当前运行时会遇到此错误:
指定的孩子已经有父母。您必须调用removeview() 首先在孩子的父母上。
I'm new to android development. From my activity.java file, I am checking if create_task menu item is clicked. If it is clicked, i want to show a pop up screen create_task(layout in xml).
activity.java
case R.id.create_task:
CreateTaskFragment createTaskFragment = new CreateTaskFragment();
FragmentTransaction createTaskft = getSupportFragmentManager().beginTransaction();
createTaskft.replace(R.id.flContent, createTaskFragment);
createTaskft.commit();
CreateTaskFragment.java
public class CreateTaskFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.create_task, container, false);
AlertDialog newTaskdialog = new AlertDialog.Builder(getContext())
.setView(view)
.create();
newTaskdialog.show();
newTaskdialog.setCanceledOnTouchOutside(true);
return view;
}
}
I get this error when i run it currently:
The specified child already has a parent. You must call removeView()
on the child's parent first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)