尝试将int值从对话框传递回活动时发生ClassCastException

时间:2019-03-14 16:44:34

标签: java android android-dialogfragment

我做了一个dialogFragment,当单击“确定”按钮时,它会将整数值发送回游戏屏幕,这是我认为的活动。但是,当我尝试运行该应用程序时,它因ClassCastException崩溃。我看过一些在线示例,这些示例告诉我使用已经完成的界面,但是我不知道为什么要得到此异常,因为我已经在游戏屏幕中实现了该界面。

public class AddEmployeesDialogFragment extends DialogFragment {

    public static interface DialogListener {
        void onNeutralClick(DialogFragment dialog, int val);
    }

    //variables
    int value = 0;
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    DialogListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        String regionName = getArguments().getString("regionName");

        builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("How many Employees do you want to add to " + regionName + "?");
        builder.setMessage((new Integer(value)).toString());
        builder.setPositiveButton("+", null);
        builder.setNegativeButton("---", null);
        builder.setNeutralButton("Ok", null);

        // Create the AlertDialog object and return it
        return builder.create();
    }

    @Override
    public void onStart(){
        super.onStart();
        alertDialog = (AlertDialog)getDialog();
        if(alertDialog != null)
        {
            Button positiveButton = (Button)alertDialog.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    value++;
                    alertDialog.setMessage((new Integer(value)).toString());
                }
            });
            Button negativeButton = (Button)alertDialog.getButton(Dialog.BUTTON_NEGATIVE);
            negativeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(value > 0) {
                        value--;
                    }
                    alertDialog.setMessage((new Integer(value)).toString());
                }
            });
            Button neutralButton = (Button)alertDialog.getButton(Dialog.BUTTON_NEUTRAL);
            neutralButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onNeutralClick(AddEmployeesDialogFragment.this, value);
                    dismiss();
                }
            });
        }
    }

    @Override
    public  void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (DialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement DialogListener");
        }
    }
}
public class SoftwarGameScreen extends GameScreen implements AddEmployeesDialogFragment.DialogListener{

//other code here...

    @Override
    public void onNeutralClick(DialogFragment dialog, int val) {
        regionValue[4] = (new Integer(val)).toString();
    }

}
E/AndroidRuntime: FATAL EXCEPTION: main
    PID: 2196
    java.lang.ClassCastException: MainActivity@b424f98 must implement DialogListener
        at AddEmployeesDialogFragment.onAttach(AddEmployeesDialogFragment.java:85)
        at android.app.Fragment.onAttach(Fragment.java:1478)
        at android.app.DialogFragment.onAttach(DialogFragment.java:370)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1242)
        at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2426)
        at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2205)
        at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2161)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2062)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:738)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

您将必须在MainActivity.java文件中实现DialogListener接口。 尝试写作:

    public class MainActivity extends AppCompatActivity implements AddEmployeesDialogFragment.DialogListener{

//other code here...

    @Override
    public void onNeutralClick(DialogFragment dialog, int val) {
        regionValue[4] = (new Integer(val)).toString();
    }

}