我在我的应用程序中创建了DialogFragment。这是代码:
public static class SkipDialogFragment extends DialogFragment implements
OnClickListener {
private Button btnYes;
private Button btnCancel;
//private PerformActionListener mListener;
private AlertDialog ad;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//mListener = (PerformActionListener) getActivity();
LayoutInflater li = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customDialogView = li.inflate(R.layout.dialog_alert_yes_no,
null);
TextView txtMessage = (TextView) customDialogView
.findViewById(R.id.txtMessage);
txtMessage.setText(getResources().getString(
R.string.disagreement_stmt));
btnYes = (Button) customDialogView.findViewById(R.id.btnOk);
btnYes.setOnClickListener(this);
btnCancel = (Button) customDialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(this);
ad = new AlertDialog.Builder(getActivity()).setView(
customDialogView).create();
return ad;
}
@Override
public void onClick(View v) {
if (v.equals(btnYes)) {
new CreateProfileFragment().gotoHome();
//mListener.onActionPerformed();
ad.dismiss();
}
if (v.equals(btnCancel)) {
ad.dismiss();
}
}
}
当用户点击对话框中的“正面”按钮时,它应该导航到另一个片段。以下是要显示的片段的代码:
private void gotoHome() {
HomeFragment homeFragment = (HomeFragment) getFragmentManager()
.findFragmentById(R.id.home_fragment);
if (homeFragment != null) {
homeFragment.initializeView();
} else {
homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.replace(R.id.linFragmentContainer, homeFragment,
AppUtils.HOME_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}
}
但是,每当我点击对话框中的Yes按钮时,gotoHome()中的getFragmentManager()都会返回null。我不明白为什么会这样。