Java MainActivity必须实现AlertPositiveListener

Java MainActivity必须实现AlertPositiveListener,java,android,radio-button,android-alertdialog,Java,Android,Radio Button,Android Alertdialog,我正在尝试创建一个showAddForm按钮,如下图所示 单击+按钮时,将显示带有单选按钮的警报对话框窗口 Claims.java public class Claims extends Fragment { Intent intent; int position=0; public View onCreateView(LayoutInflater inflater, ViewGroup container,

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

单击+按钮时,将显示带有单选按钮的警报对话框窗口

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;
    }
}
public class Android {

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

    };
}
Android.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;
    }
}
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$1.run(FragmentManager.java:443)
已编辑


如何解决此问题?

附加了
AlertDialogRadio
片段的活动必须实现上述接口

这是在
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");
    }
}
要解决此问题,请将
实现AlertPositiveListener
添加到活动中,并实现回调方法

MainActivity@422ab848必须实现AlertPositiveListener

因为
main活动
声明
片段均未实现
AlertPositiveListener
接口,而是尝试将
活动
强制转换为
AlertPositiveListener

MainActivity
中实现
AlertPositiveListener
,或者最好在
Claimes
Fragment中实现,因为如果要在显示
DialogFragment
的片段中获取回调

1.
声明
片段中实施
AlertPositiveListener

  public class Claims extends Fragment  implement AlertPositiveListener{ 

     .....
   } 
AlertDialogRadio alert = new AlertDialogRadio();
alert.setListener(this);
2.
AlertDialogRadio
片段中创建一个setListener方法:

public void setListener(AlertPositiveListener alertPositiveListener){
 this.alertPositiveListener=alertPositiveListener;
}
3.调用
setListener
from
声明
片段:

  public class Claims extends Fragment  implement AlertPositiveListener{ 

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

并且还将
AlertPositiveListener
接口声明为
public

如何将AlertPositiveListener接口声明为public?就像我在AlertDialogRadio中所做的那样?@Hoo:以公共界面AlertPositiveListener的方式进行操作。如果不起作用,最好在单独的界面中创建
AlertPositiveListener
接口file@Hoo:在“接口AlertDialogRadio.AlertPositiveListener”之前添加
public
,并使用“AlertDialogRadio.AlertPositiveListener”您的意思是公共类声明扩展了AlertDialogRadio.AlertPositiveListener吗{???@Hoo:Yes,并使用
公共接口AlertPositiveListener{public void onPositiveClick(int位置);}