我试图创建一个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;
}
}
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$1.run(FragmentManager.java:443)
已编辑
如何解决这个问题?
最佳答案
MainActivity @ 422ab848必须实现AlertPositiveListener
因为MainActivity
或Claims
片段都没有实现AlertPositiveListener
接口,而是试图将Activity
强制转换为AlertPositiveListener
。
在AlertPositiveListener
中实现MainActivity
或在Claims
Fragment中实现最佳都是因为要在Fragment中获取回调,从中显示DialogFragment
。
1.在AlertPositiveListener
片段中实现Claims
:
public class Claims extends Fragment implement AlertPositiveListener{
.....
}
2.在
AlertDialogRadio
Fragment中创建一个setListener方法:public void setListener(AlertPositiveListener alertPositiveListener){
this.alertPositiveListener=alertPositiveListener;
}
3.从
setListener
片段调用Claims
:AlertDialogRadio alert = new AlertDialogRadio();
alert.setListener(this);
并将
AlertPositiveListener
接口声明为public