I'am trying to use wizaredPager in a fragment, and i got "Activity must implement PageFragmentCallbacks" exception .
`
public class CollectionDonneesFragment extends Fragment implements
PageFragmentCallbacks, ReviewFragment.Callbacks, ModelCallbacks
{
private ViewPager mPager;
private MyPagerAdapter mPagerAdapter;
private boolean mEditingAfterReview;
private AbstractWizardModel mWizardModel = new NotificationWizardModel(getContext());
private boolean mConsumePageSelectedEvent;
private Button mNextButton;
private Button mPrevButton;
private List<Page> mCurrentPageSequence;
private StepPagerStrip mStepPagerStrip;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_collection_donnees, container, false);
if (savedInstanceState != null) {
mWizardModel.load(savedInstanceState.getBundle("model"));
}
mWizardModel.registerListener(this);
mPagerAdapter = new MyPagerAdapter(getFragmentManager());
mPager = (ViewPager) view.findViewById(R.id.pager);
mPager.setAdapter(mPagerAdapter);
mStepPagerStrip = (StepPagerStrip) view.findViewById(R.id.strip);
mStepPagerStrip
.setOnPageSelectedListener(new StepPagerStrip.OnPageSelectedListener() {
@Override
public void onPageStripSelected(int position) {
position = Math.min(mPagerAdapter.getCount() - 1,
position);
if (mPager.getCurrentItem() != position) {
mPager.setCurrentItem(position);
}
}
});
mNextButton = (Button) view.findViewById(R.id.next_button);
mPrevButton = (Button) view.findViewById(R.id.prev_button);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mStepPagerStrip.setCurrentPage(position);
if (mConsumePageSelectedEvent) {
mConsumePageSelectedEvent = false;
return;
}
mEditingAfterReview = false;
updateBottomBar();
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
DialogFragment dg = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage("Envoyer vos données ? Activez la reception des notification et suivez toutes les nouveautés de votre opérateur")
.setPositiveButton(
"Envoyer vos données",
null)
.setNegativeButton(android.R.string.cancel,
null).create();
}
};
dg.show(getFragmentManager(), "place_order_dialog");
} else {
if (mEditingAfterReview) {
mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
} else {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
}
});
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
});
onPageTreeChanged();
updateBottomBar();
return view;
}
@Override
public void onPageTreeChanged() {
mCurrentPageSequence = mWizardModel.getCurrentPageSequence();
recalculateCutOffPage();
mStepPagerStrip.setPageCount(mCurrentPageSequence.size() + 1); // + 1 =
// review
// step
mPagerAdapter.notifyDataSetChanged();
updateBottomBar();
}
private void updateBottomBar() {
int position = mPager.getCurrentItem();
if (position == mCurrentPageSequence.size()) {
mNextButton.setText("Finir");
mNextButton.setBackgroundResource(R.drawable.finish_background);
mNextButton.setTextAppearance(getContext(), R.style.TextAppearanceFinish);
} else {
mNextButton.setText(mEditingAfterReview ? R.string.review
: R.string.next);
mNextButton
.setBackgroundResource(R.drawable.selectable_item_background);
TypedValue v = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.textAppearanceMedium, v,
true);
mNextButton.setTextAppearance(getContext(), v.resourceId);
mNextButton.setEnabled(position != mPagerAdapter.getCutOffPage());
}
mPrevButton
.setVisibility(position <= 0 ? View.INVISIBLE : View.VISIBLE);
}
@Override
public AbstractWizardModel onGetModel() {
return mWizardModel;
}
@Override
public void onEditScreenAfterReview(String key) {
for (int i = mCurrentPageSequence.size() - 1; i >= 0; i--) {
if (mCurrentPageSequence.get(i).getKey().equals(key)) {
mConsumePageSelectedEvent = true;
mEditingAfterReview = true;
mPager.setCurrentItem(i);
updateBottomBar();
break;
}
}
}
@Override
public void onPageDataChanged(Page page) {
if (page.isRequired()) {
if (recalculateCutOffPage()) {
mPagerAdapter.notifyDataSetChanged();
updateBottomBar();
}
}
}
@Override
public Page onGetPage(String key) {
return mWizardModel.findByKey(key);
}
private boolean recalculateCutOffPage() {
// Cut off the pager adapter at first required page that isn't completed
int cutOffPage = mCurrentPageSequence.size() + 1;
for (int i = 0; i < mCurrentPageSequence.size(); i++) {
Page page = mCurrentPageSequence.get(i);
if (page.isRequired() && !page.isCompleted()) {
cutOffPage = i;
break;
}
}
if (mPagerAdapter.getCutOffPage() != cutOffPage) {
mPagerAdapter.setCutOffPage(cutOffPage);
return true;
}
return false;
}
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private int mCutOffPage;
private Fragment mPrimaryItem;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
if (i >= mCurrentPageSequence.size()) {
return new ReviewFragment();
}
return mCurrentPageSequence.get(i).createFragment();
}
@Override
public int getItemPosition(Object object) {
// TODO: be smarter about this
if (object == mPrimaryItem) {
// Re-use the current fragment (its position never changes)
return POSITION_UNCHANGED;
}
return POSITION_NONE;
}
@Override
public void setPrimaryItem(ViewGroup container, int position,
Object object) {
super.setPrimaryItem(container, position, object);
mPrimaryItem = (Fragment) object;
}
@Override
public int getCount() {
return Math.min(mCutOffPage + 1, mCurrentPageSequence == null ? 1
: mCurrentPageSequence.size() + 1);
}
public void setCutOffPage(int cutOffPage) {
if (cutOffPage < 0) {
cutOffPage = Integer.MAX_VALUE;
}
mCutOffPage = cutOffPage;
}
public int getCutOffPage() {
return mCutOffPage;
}
}
}
and this is the logcat
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: Activity must implement PageFragmentCallbacks
at com.tech.freak.wizardpager.ui.SingleChoiceFragment.onAttach(SingleChoiceFragment.java:106)
at android.support.v4.app.Fragment.onAttach(Fragment.java:1196)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1281)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1627)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
at android.view.View.measure(View.java:12865)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12865)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1079)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
at android.view.View.measure(View.java:12865)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2242)
at android.view.View.measure(View.java:12865)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2632)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)`