java - 如何在Fragment类中使用Dialog

标签 java android android-fragments

我希望将自定义弹出窗口显示在 fragment 中。不幸的是,对象对话框没有引用正确的布局。我怎样才能让它指向正确的布局?我希望有人能向我解释错误在哪里,以便我可以调用它。

public class UserFragmentBestell<Textview> extends Fragment {
    
        RecyclerView recyclerView;
        TextView notfound;
        Dialog epicDialog;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.user_fragment_bestell, container, false);
            recyclerView = view.findViewById(R.id.recyclerview_scroll);
            notfound = view.findViewById(R.id.user_order_notfound);
            getBestellungen();
            epicDialog = new Dialog(getActivity());
    
            return view;
    
        }
    
        
    
        public void callAction(int pId) {
            System.out.println(pId);
            epicDialog.setContentView(R.layout.user_popup_order_overview);
            epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
            //System.out.println(bestellung.get(position).getBestellnummer());
            btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    epicDialog.dismiss();
                }
    
            });
            epicDialog.show();
        }
    
    }

最佳答案

documentation建议您不要在您的情况下直接实例化对话框,而是使用 AlterDialog

   public void callAction(int pId){ // idk what you're doing with pid
    new AlertDialog.Builder(getActivity())
     .setMessage("message") //set the message on the dialog
     .setView(R.layout.user_popup_order_overview) // set layout as view of this dialog
     .setNegativeButton("cancel",null) // pressing cancel will dismiss this dialog
      }).create().show() // create and show this dialog;
   }

祝你有美好的一天

关于java - 如何在Fragment类中使用Dialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62486865/

相关文章:

java - 在 Java 中,什么时候应该使用 "Object o"而不是泛型?

Android通过蓝牙接收接收文本

java - 如何在 Firebase 中获取特定的 PushID?

java - 将数据加载到 fragment 中,然后在其他类中使用该数据

android - 使用 DataBinding 测试 fragment 时错误膨胀类 fragment

android - 半透明/透明状态栏 + CoordinatorLayout + Toolbar + Fragment

java - 如何使用恢复按钮进入上一个 Activity ?

java - java.io.* 中 * 的用途是什么

java - 客户端无法从服务器接收信息

java - 使用 Android Studio 格式化 JAVA 中的字符串