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

  • Home
  • SEARCH
  • 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 8622157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:59:03+00:00 2026-06-12T06:59:03+00:00

Attempting to use a ViewPager with FragmentPagerAdapter with all dynamic GUI elements (i.e. no

  • 0

Attempting to use a ViewPager with FragmentPagerAdapter with all dynamic GUI elements (i.e. no XML). Yes I have a good reason and not just because I feel like it 🙂 Cannot find anything in the docs that suggests this should not work.

The only difference between working and not working is having the ViewGroup (LinearLayout) and ViewPager defined in XML as opposed to created in the onCreate(). Gives the following error:

java.lang.IllegalArgumentException: No view found for id 0xffffffff for fragment PageFragment{41936480 #0 id=0xffffffff android:switcher:-1:0}

Code:

public class DynoPagerActivity extends FragmentActivity {

  private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {
        return PageFragment.newInstance("My Message " + index);
    }

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

  private ViewPager mViewPager;
  private MyFragmentPagerAdapter mMyFragmentPagerAdapter;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    setContentView(ll);

    mViewPager = new ViewPager(this);
    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mMyFragmentPagerAdapter);
    ll.addView(mViewPager);
    //setContentView(ll);  tried here as well...
  }
}

and:

public class PageFragment extends Fragment {

public static PageFragment newInstance(String title) {

    PageFragment pageFragment = new PageFragment();
    Bundle bundle = new Bundle();
    bundle.putString("title", title);
    pageFragment.setArguments(bundle);
    return pageFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // https://android-review.googlesource.com/#/c/31261/  - bug in Support lib
    setUserVisibleHint(true);
}

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

    TextView textView = new TextView(container.getContext());
    textView.setText(getArguments().getString("title"));
    return textView;
}
}

Seems to be an issue with how/when fragments are created, but failing to find the connection…

  • 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-12T06:59:04+00:00Added an answer on June 12, 2026 at 6:59 am

    The ViewPager results in a call to findViewById() for each “page” which tries to get the parent (the ViewPager ID), which always fails for Views that do not have their ID set. When you programmatically create Views, they get an ID of -1, which after browsing the Android platform source code, always returns null/fail for findViewByID().

    Adding an ID makes it work, but then you need to manage IDs yourself safely to avoid issues. Add this to code above to make it work in the onCreate() method:

    mViewPager.setId(888889);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to use ASP.NET MVC remote validation for username login access
I am attempting to use XMLParse against content that is not valid xhtml. In
I am attempting to use the haml-jekyll-extension only I do not understand how to
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
Attempting to use squarebracket notation to reference a dynamic variable. (I'm looping through a
While attempting to use the CopyFile() function I have encountered a strange error. It
I am attempting to use Ant's XMLValidate task to validate an XML document against
I am attempting to use MailCore in an app, I have gotten the project
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng

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.