Note that there are some explanatory texts on larger screens.

plurals
  1. POViewPager with one page containing multiple Fragments "java.lang.IllegalArgumentException: No view found for id"
    text
    copied!<p>In my small Android app, I have a <code>ViewPager</code> with 3 pages, each page contains one fragment, that's all working fine with a <code>FragmentStatePagerAdapter</code> (see <a href="https://stackoverflow.com/a/13597181/347565">earlier question</a>).</p> <p>Now for larger screens, I would like to keep a <code>ViewPager</code>, but I want the first two fragments to be on the same first page.</p> <p>For this, I implement my own PagerAdapter and this is mostly working, the only problem I am trying to resolve is when I am rotating the screen, I get an error:</p> <pre><code>12-11 16:34:13.526: ERROR/AndroidRuntime(9221): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthieu/com.matthieu.TestActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f030002 (com.matthieu:id/top_fragment) for fragment TestFragment{481a3a98 #0 id=0x7f030002} at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815) at android.app.ActivityThread.access$2400(ActivityThread.java:125) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f030002 (com.matthieu:id/top_fragment) for fragment TestFragment{481a3a98 #0 id=0x7f030002} at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) at android.app.Activity.performStart(Activity.java:3781) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) ... 12 more </code></pre> <p>I put a sample code simulating what I am trying to do (and showing this error message) on <a href="https://github.com/MatthieuLJ/fragment_layout" rel="nofollow noreferrer">GitHub</a>.</p> <p>I guess one problem I have is because of the Fragments I have in the real application, I cannot really have a <code>static newInstance</code> constructor, but instead the Fragments are created ahead of time and have 'setRetainInstance(true);' set.</p> <p>I understand the id of the <code>viewGroup</code> that will contain the fragment <a href="https://stackoverflow.com/a/8158916/347565">should be part of the hierarchy</a>, and eventually, I guess, it is...</p> <p>If it makes any difference, I am using the support library and the SherlockActionBar to maintain backwards compatibility with older devices.</p> <p>Any idea on how to go about this?</p> <p><strong>EDIT</strong>: I have found that if I remove the Fragments from the <code>FragmentManager</code> in <code>onSaveInstanceState</code> I am not getting any crash anymore. All the fragments are recreated from scratch which is not what I want, but I guess I might have to do this and somehow recover those fragments later....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload