在片段的onCreateview()上显示警报对话框

发布于 2025-01-18 10:12:01 字数 1141 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灰色世界里的红玫瑰 2025-01-25 10:12:01
  AlertDialog alertDialog1 = new AlertDialog.Builder(
                getActivity()).create();

        alertDialog1.setTitle("Alert Dialog");
        alertDialog1.setMessage("this is alert dialog box");
        alertDialog1.setIcon(R.drawable.tick);
        alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(),
                        "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
        });

        alertDialog1.show();
  AlertDialog alertDialog1 = new AlertDialog.Builder(
                getActivity()).create();

        alertDialog1.setTitle("Alert Dialog");
        alertDialog1.setMessage("this is alert dialog box");
        alertDialog1.setIcon(R.drawable.tick);
        alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(),
                        "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
        });

        alertDialog1.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文