MainActivity 必须实现 AlertPositiveListener

MainActivity must implement AlertPositiveListener

我正在尝试创建一个如下图所示的 showAddForm 按钮。

单击 + 按钮后,将显示带有单选按钮的警告对话框 window。

Claims.java

public class Claims extends Fragment  {
    Intent intent;
    int position=0;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View claims= inflater.inflate(R.layout.claims, container, false);
        View.OnClickListener listener =new View.OnClickListener()
        {
            public void onClick(View v)
            {
                FragmentManager manager =getFragmentManager();
                AlertDialogRadio alert = new AlertDialogRadio();
                /** Creating a bundle object to store the selected item's index */
                Bundle b  = new Bundle();

                /** Storing the selected item's index in the bundle object */
                b.putInt("position", position);

                /** Setting the bundle object to the dialog fragment object */
                alert.setArguments(b);

                /** Creating the dialog fragment object, which will in turn open the alert dialog window */
                alert.show(manager, "alert_dialog_radio");
            }
        };
        Button button1=(Button)claims.findViewById(R.id.button10);
        Button button=(Button)claims.findViewById(R.id.button8);
       button1.setOnClickListener(listener);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
                startActivity(intent);
            }
        });
        return claims;
    }
}

AlertDialogRadio.java

public class AlertDialogRadio extends DialogFragment {
    /** Declaring the interface, to invoke a callback function in the implementing activity class */
    AlertPositiveListener alertPositiveListener;

    /** An interface to be implemented in the hosting activity for "OK" button click listener */
    interface AlertPositiveListener {
        public void onPositiveClick(int position);
    }

    /** This is a callback method executed when this fragment is attached to an activity.
     *  This function ensures that, the hosting activity implements the interface AlertPositiveListener
     * */
    public void onAttach(android.app.Activity activity) {
        super.onAttach(activity);
        try{
            alertPositiveListener = (AlertPositiveListener) activity;
        }catch(ClassCastException e){
            // The hosting activity does not implemented the interface AlertPositiveListener
            throw new ClassCastException(activity.toString() + " must implement AlertPositiveListener");
        }
    }

    /** This is the OK button listener for the alert dialog,
     *  which in turn invokes the method onPositiveClick(position)
     *  of the hosting activity which is supposed to implement it
     */
    OnClickListener positiveListener = new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            AlertDialog alert = (AlertDialog)dialog;
            int position = alert.getListView().getCheckedItemPosition();
            alertPositiveListener.onPositiveClick(position);
        }
    };

    /** This is a callback method which will be executed
     *  on creating this fragment
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        /** Getting the arguments passed to this fragment */
        Bundle bundle = getArguments();
        int position = bundle.getInt("position");

        /** Creating a builder for the alert dialog window */
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());

        /** Setting a title for the window */
        b.setTitle("Choose your version");

        /** Setting items to the alert dialog */
        b.setSingleChoiceItems(Android.code, position, null);

        /** Setting a positive button and its listener */
        b.setPositiveButton("OK",positiveListener);

        /** Setting a positive button and its listener */
        b.setNegativeButton("Cancel", null);

        /** Creating the alert dialog window using the builder class */
        AlertDialog d = b.create();

        /** Return the alert dialog window */
        return d;
    }
}

Android.java

public class Android {

    static String[] code = new String[]{
            "Project",
            "Petrol",
            "Medical",

    };
}

不幸的是,当点击 + 按钮时,应用程序崩溃了。

LogCat错误

10-24 23:18:19.500    9033-9033/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 9033
    java.lang.ClassCastException: com.example.project.project.MainActivity@422ab848 must implement AlertPositiveListener
            at com.example.project.project.AlertDialogRadio.onAttach(AlertDialogRadio.java:31)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
            at android.app.FragmentManagerImpl.run(FragmentManager.java:443)

已编辑

如何解决这个问题?

附加 AlertDialogRadio 片段的 Activity 必须实现上述接口。

这是在您的 AlertDialogRadio 片段代码中强制执行的:

public void onAttach(android.app.Activity activity) {
    super.onAttach(activity);
    try{
        alertPositiveListener = (AlertPositiveListener) activity;
    }catch(ClassCastException e){
        // The hosting activity does not implemented the interface AlertPositiveListener
        throw new ClassCastException(activity.toString() + " must implement AlertPositiveListener");
    }
}

要解决此问题,请将 implements AlertPositiveListener 添加到您的 activity 并实施回调方法

MainActivity@422ab848 must implement AlertPositiveListener

因为 MainActivityClaims 片段都没有实现 AlertPositiveListener 接口但试图将 Activity 转换为 AlertPositiveListener.

要么在 MainActivity 中实现 AlertPositiveListener,要么最好在 Claims 片段中实现,因为如果想在显示 DialogFragment.[=30= 的片段中获得回调]

1.Claims 片段中实施 AlertPositiveListener 片段:

  public class Claims extends Fragment  implement AlertPositiveListener{ 

     .....
   } 

2.AlertDialogRadio片段中创建一个setListener方法:

public void setListener(AlertPositiveListener alertPositiveListener){
 this.alertPositiveListener=alertPositiveListener;
}

3.Claims 调用 setListener 片段:

AlertDialogRadio alert = new AlertDialogRadio();
alert.setListener(this);

并且还将 AlertPositiveListener 接口声明为 public