title: How can I get (or emulate) a ViewPager inside a DialogFragment?
body: <p>I want to have a <code>dialogFragment</code> with a few pages, to enter some settings for my app. When I use <code>AlertDialog.Builder</code>, I get an <code>IllegalStateException</code> saying &quot;Fragment does not have a view&quot;, which confuses me (also the stack is pretty useless).</p>
  <p>I can get a popup working with the <code>ViewPager</code> in it, doing exactly what I want it to do, but it doesn't have the buttons I want. I really like the look of the default <code>AlertDialog</code>, so I'd preferably use that if possible (for consistency reasons). If not, I'd be okay with making a custom dialog that just looks exactly the same.</p>
  <p>So, how can I get the <code>ViewPager</code> working in a <code>AlertDialog / dialogFragment</code> (or similar)?</p>
  <p><strong>EDIT:</strong> <em>Just to clarify, it doesn't have to be an <code>AlertDialog</code>, but I would like the option to set a title, (permanent) message and buttons.</em></p>
  <p><strong>EDIT2:</strong> <em>So it appears the &quot;Fragment does not have a view&quot; error occurs on returning <code>f</code> in <code>FragmentFirst newInstance()</code>. This should return a fragment to the <code>getItem()</code> method in the <code>adapter</code>, which should return the fragment associated with the correct position. I hope this helps anyone pinpointing what's going wrong.</em></p>
  <p><strong>EDIT3:</strong> <em>Upon messing around with some stuff, I now got this error: <code>No view found for id 0x7f0c00b7 (com.example.tim.timapp:id/pager) for fragment FragmentDefault{47febfa #1 id=0x7f0c00b7 android:switcher:2131493047:0}</code>, basically telling me something is trying to find the pager view in the wrong fragment? Not quite sure how to fix that. NB. <code>FragmentDefault</code> is exactly the same as <code>FragmentFirst</code>, except for its name and some id's.</em></p>
  <p><strong>EDIT4:</strong> (11 Mar '16) <em>A question: When using a <code>newInstance</code> method (like I do inside the fragments), should the returned fragment have a view? When I call <code>if (f.getView() == null)</code>, it returns true, which makes me wonder if <code>f</code> even should have a view. If so, why doesn't it? Every <code>newInstance</code> method I see online has exactly the same layout as mine, so it doesn't look like I'm missing something.<br />
  Also, for some reason <code>onCreateView</code> in the fragments (FragmentFirst, FragmentSecond, etc.) is never reached. I have a <code>Log.d</code> right after the <code>onCreateView</code>, which never shows in the logcat. Any ideas?</em></p>
  <p>Code:<br />
  <strong>Calling the dialogFragment</strong></p>
  <pre><code>TestActivityFragment newFragment = new TestActivityFragment();  
  newFragment.show(getChildFragmentManager(), &quot;dialog&quot;);  
  </code></pre>
  <p><strong>TestActivityFragment:</strong></p>
  <pre><code>package com.example.fragments.MainFragments.Test;
  
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.DialogFragment;
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.content.Context;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v13.app.FragmentStatePagerAdapter;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.util.AttributeSet;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.example.tim.timapp.R;
  
  public class TestActivityFragment extends DialogFragment {
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          LayoutInflater inflater = getActivity().getLayoutInflater();
          final View view = inflater.inflate(R.layout.test_dialog, null);
  
          final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder.setView(view)
                  .setPositiveButton(&quot;POS&quot;, null)
                  .setNegativeButton(&quot;NEG&quot;, null)
                  .setTitle(&quot;TEST_TITLE&quot;)
                  .setMessage(&quot;TEST_MESSAGE&quot;);
  
          ViewPager mPager = (ViewPager) view.findViewById(R.id.pager);
          FragmentStatePagerAdapter mPagerAdapter = new TestAdapter(getChildFragmentManager());
          mPager.setAdapter(mPagerAdapter);
  
          return builder.create();
      }
  
      private class TestAdapter extends FragmentStatePagerAdapter {
          public TestAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position) {
              switch(position) {
                  case 0: return FragmentFirst.newInstance(&quot;FragmentFirst, Instance 1&quot;);
                  case 1: return FragmentSecond.newInstance(&quot;SecondFragment, Instance 1&quot;);
                  case 2: return FragmentThird.newInstance(&quot;ThirdFragment, Instance 1&quot;);
                  case 3: return FragmentThird.newInstance(&quot;ThirdFragment, Instance 2&quot;);
                  default: return FragmentDefault.newInstance(&quot;DefaultFragment&quot;);
              }
          }
  
          @Override
          public int getCount() {
              return 5;
          }
      }
  }
  </code></pre>
  <p><strong>One of the fragments</strong></p>
  <pre><code>package com.example.fragments.MainFragments.Test;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  import com.example.tim.timapp.R;
  
  public class FragmentFirst extends Fragment{
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.frag_first, container, false);
  
          TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
          tv.setText(getArguments().getString(&quot;msg&quot;));
  
          return v;
      }
  
      public static FragmentFirst newInstance(String text) {
  
          FragmentFirst f = new FragmentFirst();
          Bundle b = new Bundle();
          b.putString(&quot;msg&quot;, text);
  
          f.setArguments(b);
  
          return f;
      }
  }
  </code></pre>
  <hr />
  <h1>XML's</h1>
  <p><strong>test_dialog.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;LinearLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id=&quot;@+id/pager&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>(I've had the viewPager as parent layout as well, without anything in it, didn't change a thing)</p>
  <p><strong>frag_first.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;TextView
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:id=&quot;@+id/tvFragFirst&quot;
          android:layout_centerVertical=&quot;true&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:text=&quot;TextView&quot;
          android:textAppearance=&quot;?android:textAppearanceMedium&quot;/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <hr />
  <h1>Log</h1>
  <pre><code>03-09 15:30:55.910 23343-23343/? I/art: Not late-enabling -Xcheck:jni (already on)
  03-09 15:30:55.940 23343-23349/? E/art: Failed sending reply to debugger: Broken pipe
  03-09 15:30:55.940 23343-23349/? I/art: Debugger is no longer active
  03-09 15:30:55.950 23343-23343/? W/System: ClassLoader referenced unknown path: /data/app/com.example.tim.timapp-2/lib/x86_64
  03-09 15:30:56.060 23343-23356/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
  03-09 15:30:56.150 23343-23356/? I/OpenGLRenderer: Initialized EGL, version 1.4
  03-09 15:30:56.210 23343-23356/? W/EGL_emulation: eglSurfaceAttrib not implemented
  03-09 15:30:56.210 23343-23356/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f5f5597db40, error=EGL_SUCCESS
  03-09 15:30:58.870 23343-23343/com.example.tim.timapp D/GSF - getChildCount: 0
  03-09 15:30:58.910 23343-23343/com.example.tim.timapp D/AndroidRuntime: Shutting down VM
  03-09 15:30:58.910 23343-23343/com.example.tim.timapp E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.tim.timapp, PID: 23343
      java.lang.IllegalStateException: Fragment does not have a view
          at android.app.Fragment$1.onFindViewById(Fragment.java:2181)
          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
          at android.app.BackStackRecord.run(BackStackRecord.java:793)
          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
          at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:562)
          at android.support.v13.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:168)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
          at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
          at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
          at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
          at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
          at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
          at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
          at android.view.View.measure(View.java:18788)
          at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
          at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
          at android.view.Choreographer.doCallbacks(Choreographer.java:670)
          at android.view.Choreographer.doFrame(Choreographer.java:606)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5417)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  03-09 15:35:59.030 23343-23343/? I/Process: Sending signal. PID: 23343 SIG: 9
  </code></pre>
  <p>PS. If I've forgotten to add a piece of code, please let me know, so I can add it.</p>