Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager in DialogFragment, IllegalStateException: Fragment does not have a view

EDIT3: it seems that only API17 and above will have ViewPager working properly with ChildFragmentManager.........

I'm trying to add a ViewPager to my DialogFragment:

public class FirstLaunchDialogFragment extends DialogFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_Sherlock_Light_Dialog);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final View v = getActivity().getLayoutInflater().inflate(
            R.layout.fragment_dialog_first_launch, null);
    ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);

    viewPager.setAdapter(new FirstLaunchFragmentsAdapter(getChildFragmentManager()));

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v).setInverseBackgroundForced(true);
    return builder.create();
}
}

and for some reasons, it would crash saying :

04-06 00:00:49.600: E/AndroidRuntime(3734): java.lang.IllegalStateException: Fragment does not have a view
04-06 00:00:49.600: E/AndroidRuntime(3734):     at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425)
04-06 00:00:49.600: E/AndroidRuntime(3734):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)
04-06 00:00:49.600: E/AndroidRuntime(3734):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-06 00:00:49.600: E/AndroidRuntime(3734):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-06 00:00:49.600: E/AndroidRuntime(3734):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-06 00:00:49.600: E/AndroidRuntime(3734):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
.....

I've been googling for Fragment does not have a view exception for half a day now with no result

It seems to me that the Adapter could not inflate the layout or something...

Here's my Fragment and Adapter

public static class UserWelcomeFragment extends Fragment {

    public static UserWelcomeFragment newInstance() {
        return new UserWelcomeFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_welcome_user, container, false);

        return v;
    }

}

private class FirstLaunchFragmentsAdapter extends FragmentPagerAdapter {

    public FirstLaunchFragmentsAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {
        case 0:
            return UserWelcomeFragment.newInstance();

        case 1:
            return new IntegrationSelectingDialogFragment();

        default:
            return UserWelcomeFragment.newInstance();
        }

    }

    @Override
    public int getCount() {
        return 2;
    }

}

EDIT: https://code.google.com/p/android/issues/detail?id=42601 looks related

EDIT2: I'm testing on API 16 and the project is using support library v4 revision 12

like image 441
tom91136 Avatar asked Oct 21 '22 13:10

tom91136


1 Answers

Which Android version are you testing this on? ViewPagers use fragments, and because your ViewPager is in a fragment itself, you are nesting the fragments. Nested fragments are only supported from API 17 (Jellybean), so this implementation will only work on devices with Android 4.1+.

like image 176
al. Avatar answered Oct 24 '22 06:10

al.