我尝试创建一个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)
被修改
如何解决这个问题?
答案 0 :(得分:1)
MainActivity @ 422ab848必须实现AlertPositiveListener
由于MainActivity
或Claims
片段未实现AlertPositiveListener
界面,但试图将Activity
投射到AlertPositiveListener
。
在AlertPositiveListener
中实施MainActivity
或在Claims
片段中最好实施,因为如果想要在显示DialogFragment
的片段中获得回调。
1。在AlertPositiveListener
片段中实施Claims
:
public class Claims extends Fragment implement AlertPositiveListener{
.....
}
2. 在AlertDialogRadio
片段中创建setListener方法:
public void setListener(AlertPositiveListener alertPositiveListener){
this.alertPositiveListener=alertPositiveListener;
}
3。从setListener
片段致电Claims
:
AlertDialogRadio alert = new AlertDialogRadio();
alert.setListener(this);
并将AlertPositiveListener
接口声明为public
答案 1 :(得分:0)
附加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");
}
}
要解决此问题,请将implements AlertPositiveListener
添加到您的活动并实施回调方法