我创建了一个 AlertDialogFragment 类,并尝试使用以下代码从另一个类中显示它,但是将类型从 FragmentTranscation 更改为 FragmentManager 时不断收到错误消息。如果我将其更改为 FragmentManager,我会收到一条更改为 FragmentTranscation 的消息,每当我更改为 FragmentTranscation 时,我都会收到一条更改为 FragmentManager 的消息:
这是显示警报对话框的代码:
FragmentTransaction ft= getFragmentManager().beginTransaction();
AlertDialogFragment newFragment= new AlertDialogFragment();
newFragment.show(ft, "alertDialog");
这是该类的代码:
public class AlertDialogFragment extends android.support.v4.app.DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder
= new AlertDialog.Builder(getActivity());
builder.setMessage("Staying in Touch With The Ones You Love");
builder.setTitle("Togetherness");
builder.setCancelable(false);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}