5

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

tom91136
  • 8,662
  • 12
  • 58
  • 74

3 Answers3

1

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+.

al.
  • 212
  • 1
  • 2
  • 6
  • 9
    I'm on API 16 but I'm also using the Support Library v4, shouldn't the support library make my implementation compatible for lower API versions? – tom91136 Apr 06 '13 at 07:39
1

I got this error when I neglected to call setContentView() after rotation. I think I may have also been trying to set the fragment in onResume() -- which caused the illegal state (a fragment trying to be added to a non-existent view).

So for me, the solution as to remove

android:configChanges="keyboardHidden|orientation"

From my manifest, and to check for a savedInstanceState / Bundle in onResume (so I can be sure to let the system recreate my fragments, rather than adding them myself.

Though due to this error being somewhat cryptic, there may be more causes than my problem/solution here

Edit: a link to some simple/helpful code:

retain the fragment object while rotating

Community
  • 1
  • 1
pjco
  • 3,826
  • 25
  • 25
1

Ensure, that u return rootView in fragment's onCreateView() method.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_layout, container, false);

    getChildFragmentManager().beginTransaction().add(R.id.fragment_container, MyFragment.getInstance() ).commit();

    return rootView;
}

Otherwise, android will search fragment container in activity's XML.

AKTO
  • 106
  • 3