我的代码出了什么问题?我将课程扩展到fragment
并实现AlertDialogRadio.AlertPositiveListener from another class.
但我得到了错误,我不知道如何解决它。
Claims.java
public class Claims extends Fragment implement AlertPositiveListener {
Intent intent;
int position=0;
@Override
public void onPositiveClick(int position){
}
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 */
public 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);
}
};
public void setListener(AlertPositiveListener alertPositiveListener){
this.alertPositiveListener=alertPositiveListener;
}
/** 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;
}
}
错误
Error:(17, 8) error: Claims is not abstract and does not override abstract method onPositiveClick(int) in AlertPositiveListener
我该如何解决这个问题?感谢
从implement
更改为implements
错误(最新)
10-25 02:21:10.072 10543-10543/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 10543
java.lang.ClassCastException: com.example.project.project.MainActivity@422b7768 must implement AlertPositiveListener
at com.example.project.project.AlertDialogRadio.onAttach(AlertDialogRadio.java:32)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
答案 0 :(得分:1)
您需要覆盖声明类中的方法。无论何时在类中实现接口,都必须覆盖其方法。