自定义对话框 - 指定的子项已经有一个父项

custom dialog - specified child already has a parent

我看过其他帖子,但无法确定以下错误的正反面。 试图在 Recycler View 项目上弹出一个对话框。我确实有一个类似的对话框有效。由于第二个实例中的不同选项,我必须使用不同的对话框。


public class EditItemDialog extends AppCompatDialogFragment{
    private EditText projectAmountEditText;
    private EditDialogListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.activity_inventory_item, null);
        

        builder.setView(v)
                .setTitle("Edit Amount")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int amountChanged = Integer.parseInt(projectAmountEditText.getText().toString());

                        listener.editAmount(amountChanged);
                    }
                });

                projectAmountEditText = v.findViewById(R.id.amount_edit_text_item_activity);
                if(projectAmountEditText == null){
                    //TODO
                }
                builder.show();
            return builder.create();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        try {
            listener = (EditDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() +
                    "must implement EditDialogLister");
        }
    }

    public interface EditDialogListener{
        void editAmount(int amountChanged);
    }
}

这是logcat

  Process: com.x.x, PID: 8485
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5235)
        at android.view.ViewGroup.addView(ViewGroup.java:5064)
        at android.view.ViewGroup.addView(ViewGroup.java:5036)
    

我必须在对话框上添加一个 onDestroy 重写来释放视图。