1

I'm launching a DialogFragment which has a ViewPager in it, which in turn will have multiple fragments in it.

When i try to actually add the fragments inside the ViewPager though, I get an IllegalArgumentException saying the view is not found:

03-14 16:44:11.989    3617-3617/com.mycompany.app.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: No view found for id 0x7f090146 for fragment GuestProfileQuestionnaireStep1Fragment{5375fa28 #3 id=0x7f090146}
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:823)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
            at android.app.BackStackRecord.run(BackStackRecord.java:635)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
            at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:437)
            at android.support.v13.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1075)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            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:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

So basically I have the following flow:

Activity --> DialogFragment (has a) > ViewPager --> calls Adapter that in turn calls multiple fragments. Here's some relevant code:

DialogFragment

public class GuestProfileQuestionnaireContainerDialogFragment
        extends DialogFragment  {

    @InjectView(R.id.guest_profile_questionnaire_pager) ViewPager mProfileQuestionnairePager;

    // ...

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_guest_profile_questionnaire_container,
                                           container,
                                           false);
        ButterKnife.inject(this, layoutView);

        // ...
        setupProfileQuestionnaire();

        return layoutView;
    }

    // ...

    private void setupProfileQuestionnaire() {
        mProfileQuestionnairePager.setAdapter(new GuestProfileQuestionnairePagerAdapter(getFragmentManager()));
    }
}

layout (nothing fancy here)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <FrameLayout ...

    <TextView ...

    <android.support.v4.view.ViewPager
        android:id="@+id/guest_profile_questionnaire_pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</LinearLayout>

PagerAdapter that populates fragments

public class GuestProfileQuestionnairePagerAdapter
        extends FragmentStatePagerAdapter {

    public static final int TOTAL_NUM_OF_STEPS = 1;

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

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

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return GuestProfileQuestionnaireStep1Fragment.newInstance();
        }

        return null;
    }
}

Fragment

public class GuestProfileQuestionnaireStep1Fragment
        extends Fragment {

    private GuestProfileQuestionnaireStep1Fragment() {
        super();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_guest_profile_questionnaire_step1,
                                container,
                                false);
    }
}

I don't get an exception when my ViewPager is empty. So basically when the ViewPAger alone is inflated there's no problem. However the minute I return an actual fragment within the ViewPager, I get the exception.

KG -
  • 7,130
  • 12
  • 56
  • 72

3 Answers3

0

This turned out to be a very interesting issue. I was in the process of writing a detailed explanation.... when i found another Stack Overflow Answer with a better explanation. Duh!

This link points to a very similar problem:

The solution involves using a ChildFragmentManager. Things i learned along the way:

Community
  • 1
  • 1
KG -
  • 7,130
  • 12
  • 56
  • 72
0

1st of all you used getChildFragmentManager() for inside to inside dailogview.

but if you used getChildFragmentManager() than you should try to dailogFragment.

Than after you used FragmentDialog:

1st: DialogFragment used this

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

instend of

Dialog onCreateDialog(Bundle savedInstanceState);

2nd: set You viewpager inside Dialog Layout and make adapter for viewpager.

3rd: Used of dialogFragment:

MyCustomDialog fragment1 = new MyCustomDialog(listdata, position);
                                FragmentManager fm = act.getSupportFragmentManager();
                fragment1.show(fm, "");
0

I was having the same problem, after many searches found one particular answer and is amazing.

try this link below.

https://github.com/kiteflo/Android_Tabbed_Dialog

There is an activity calling the DialogFragment, and that DialogFragment layout contains the ViewPager. using the Adapter managed to make fragments in dialog.

NOTE: Make sure to use FragmentManager as getChildFragmentManager() in your DialogFragment on the time of creating Adapter.

Atif Mukhtiar
  • 1,186
  • 9
  • 11
  • There is an activity calling the DialogFragment, and that DialogFragment layout contains the ViewPager. using the Adapter managed to make fragments in dialog. NOTE: Make sure to use FragmentManager as getChildFragmentManager() in your DialogFragment on the time of creating Adapter. – Atif Mukhtiar Mar 24 '17 at 10:43