我創建了一個AlertDialogFragment類,我試圖用下面的代碼從另一個類中顯示它,但我不斷收到一個錯誤,將類型從FragmentTranscation更改爲FragmentManager。如果我將其更改爲FragmentManager,我得到一個消息更改爲FragmentTranscation,每當我改變FragmentTranscation,我得到一個消息更改爲FragmentManager:對話框不會顯示
這裏是展示alertDialog代碼:
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();
}
}
檢查本教程 - > http://vinaygopinath.wordpress.com/2012/11/10/alertdialog-using-fragment/ – 2013-05-09 06:59:58