Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9034203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:25:14+00:00 2026-06-16T08:25:14+00:00

In my small Android app, I have a ViewPager with 3 pages, each page

  • 0

In my small Android app, I have a ViewPager with 3 pages, each page contains one fragment, that’s all working fine with a FragmentStatePagerAdapter (see earlier question).

Now for larger screens, I would like to keep a ViewPager, but I want the first two fragments to be on the same first page.

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:

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

I put a sample code simulating what I am trying to do (and showing this error message) on GitHub.

I guess one problem I have is because of the Fragments I have in the real application, I cannot really have a static newInstance constructor, but instead the Fragments are created ahead of time and have ‘setRetainInstance(true);’ set.

I understand the id of the viewGroup that will contain the fragment should be part of the hierarchy, and eventually, I guess, it is…

If it makes any difference, I am using the support library and the SherlockActionBar to maintain backwards compatibility with older devices.

Any idea on how to go about this?

EDIT: I have found that if I remove the Fragments from the FragmentManager in onSaveInstanceState 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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:25:15+00:00Added an answer on June 16, 2026 at 8:25 am

    All right, even though I moved to another way of doing things, I don’t like to leave things incomplete…

    Going the way of the nested Fragments as suggested by Mark. The link posted in the comments goes to 404, but I found your example code about nested fragments.

    A few things I’ve learned on the way:

    • Nested fragments cannot have setRetainInstance(true), or it will get:

      java.lang.IllegalStateException: Can’t retain fragements that are nested in other fragments

    • It was a mistake to create a FrameLayout in the page of the ViewPager to hold the fragment. Looking in the Android code (and specifically the FragmentPagerAdapter), I should just use container.getId() to find where to add the Fragment. Looks like you cannot do anything more sophisticated at that level or it will break.

    • From my understanding, if the fragments hold some information (like I do) that takes a long time to load, the different ways to handle this are:

      • Use onSaveInstanceState (preferred way ?), but not possible if the Fragment is nested. The (empty) constructor will get called when needed by the framework.
      • Use setRetainInstance(true), in that case savedInstanceBundle is always null, but that Fragment (as Java object) will not be destroyed and it will still go through onAttach / onCreateView.
      • Find some other way to save and restore what you need…

    Anyway, I pushed my solution using nested fragments on github

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have a small functioning app that makes an httprequest to a server
I am working on a small Android App! I have a class MyPref public
I have a rather small amount of state in my Android app that I
I have a simple Android app with one Activity. This Activity downloads a small
I have written a small android app that grabs some data and displays it
I have got a small problem in an Android app I am working on
I have created a small and simple android app. I tried installing it on
I have android device with root and i try to implement some small app.
I want to write a small Android app with a GUI that should display
I've just finished designing a small android app and have now come to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.