tom91136
tom91136

Reputation: 8972

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

Upvotes: 5

Views: 6899

Answers (3)

AKTO
AKTO

Reputation: 106

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.

Upvotes: 1

pjco
pjco

Reputation: 3836

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

Upvotes: 1

al.
al.

Reputation: 222

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

Upvotes: 1

Related Questions