自定义对话框-指定的子级已具有父级

vcirk6k6  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(365)

我看过其他的帖子,但对下面的错误不太了解。正在尝试在“回收器”视图项上弹出一个对话框。我也有一个类似的对话。我必须使用不同的对话框,因为在第二个示例中有不同的选项。

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)
wecizke3

wecizke31#

我不得不在对话框上添加ondestroy覆盖以释放视图。

相关问题