提问者:小点点

如何在片段类中使用对话框


我想有我的自定义弹出窗口显示在一个片段。 不幸的是,对象对话框没有引用正确的布局。 我怎样才能让它指向正确的布局? 我希望有人能向我解释一下错误在哪里,这样我就可以调出来了。

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();
        }
    
    }

共1个答案

匿名用户

文档建议您不应直接实例化对话框,而是使用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;
   }

祝你今天愉快