我在AlertDialog类的onAttach方法中得到错误的CastClassException[关闭]。

问题描述 投票:0回答:1

这是我的Dialog类代码,我在我的片段中实现了这个ApplyDialog监听器。我已经在我的片段中实现了这个ApplyDialog监听器,我在onAttach方法中得到了错误。每当我打开这个对话框时,应用程序就会崩溃。我想把对话框中的一些文本保存到我的片段中。我已经从这个类中删除了一些代码来发布这个问题。

public class DiscountDialog extends AppCompatDialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.discount_dialog,null);
        builder.setView(view)
                .setTitle("Discount")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String DiscountName = select_discount_category.toString();
                        String DiscountAmount = discount_amount.getText().toString();
                        String DiscountType = discountType.toString();
                        listner.applyDialog(DiscountName,DiscountAmount,DiscountType);

                    }
                });

        new_discount_name = (EditText) view.findViewById(R.id.new_discount_name);
        discount_amount = (EditText) view.findViewById(R.id.discount_amount);
        select_discount_category = (Spinner) view.findViewById(R.id.select_discount_category);
        select_discount_type = (Spinner) view.findViewById(R.id.select_discount_type);
        add_discount_category = (ImageButton) view.findViewById(R.id.add_discount_category);

        stringlist = new ArrayList<>(Arrays.asList(discountItems));
        typeList = new ArrayList<>(Arrays.asList(discountType));

        arrayadapter = new ArrayAdapter<String>(getActivity(),R.layout.discount_dialog_text,stringlist);
        arrayadapter.setDropDownViewResource(R.layout.discount_dialog_text);
        select_discount_category.setAdapter(arrayadapter);

        typeAdapter = new ArrayAdapter<String>(getActivity(),R.layout.discount_dialog_text,typeList);
        typeAdapter.setDropDownViewResource(R.layout.discount_dialog_text);
        select_discount_type.setAdapter(typeAdapter);

        add_discount_category.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                GETTEXT = new_discount_name.getText().toString();

                stringlist.add(GETTEXT);

                arrayadapter.notifyDataSetChanged();

                Toast.makeText(getActivity(), "Item Added", Toast.LENGTH_LONG).show();

            }
        });
        return builder.create();
     }

我在这个方法中得到这个错误


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

        try {
            listner = (discountDialogListner) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + "Must Implement discountDialogListner"); //This is the line where I'm getting this error
        }
    }



    public interface discountDialogListner
    {
        void applyDialog(String DiscountName,String DiscountType,String DiscountAmount);
    }
}
java android android-fragments exception android-alertdialog
1个回答
0
投票

这是因为上下文的类型不属于 "我的"。discountDialogListner.

为了启动你的监听器,你的活动需要做到 implement 你的 interface discountDialogListner.

希望这能解决你的问题。


0
投票

我已经自己解决了,非常感谢你,我想在片段中实现这个功能,但我必须在MainActivity中实现对话框。

© www.soinside.com 2019 - 2024. All rights reserved.