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 9133209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:26:49+00:00 2026-06-17T08:26:49+00:00

I wish to dynamically create a list of fragments programmatically with subviews before adding

  • 0

I wish to dynamically create a list of fragments programmatically with subviews before adding the fragments. I have a JSON file which sets up each fragment with a list of views, so when my app starts, there is an activity called splash screen which just shows an image while the data is being processed. Quick example below of me trying to generate 3 fragments and set up their subviews (this is in the splash screen activity)

for(int i = 0; i < 3; i++)
        {
            DefaultFragment frag = new DefaultFragment();
            RelativeLayout layout = (RelativeLayout) createUI(frag);
            frag.setFragLayout(layout);
        }


private RelativeLayout createUI(final DefaultFragment frag_p) 
    {

        RelativeLayout layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        Button but = new Button(this);
        but.setWidth(100);
        but.setHeight(100);
        but.setText("Next");

        but.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0)
            {
                frag_p.testNextFragment();
            }
        });
        layoutParams.setMargins(100, 100, 0, 0);
        layout.addView((View) but, layoutParams);

        return layout;
    }

Then in the fragment there is a RelativeLayout called fraglayout

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
//took out layout inflator temp, to try using the relativeLayout which was set from the splash screen
//      View V = inflater.inflate(R.layout.fraglayout, container, false);


        return fragLayout;
    }

This allows me to add the subviews to the fragment okay, this test just involves a button that when pressed, takes you to the next fragment which works fine, but when I try to go to previous fragment, I get the error “The specified child already has a parent”.

I know this is because of this

Button but = new Button(this);

It is getting the context of the Splash Screen and not the fragment, but how do I get the context from the fragment before its creation?

Could anyone suggest to me a better method for adding the subViews to fragments prior to them being shown.

Any help would be much appreciated

01-15 12:25:01.598: E/AndroidRuntime(3443): FATAL EXCEPTION: main
01-15 12:25:01.598: E/AndroidRuntime(3443):
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child’s parent first. 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.view.ViewGroup.addViewInner(ViewGroup.java:1976) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.view.ViewGroup.addView(ViewGroup.java:1871) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.view.ViewGroup.addView(ViewGroup.java:1828) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.view.ViewGroup.addView(ViewGroup.java:1808) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
com.single.DefaultFragment.onCreateView(DefaultFragment.java:61) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
01-15 12:25:01.598: E/AndroidRuntime(3443): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
01-15 12:25:01.598: E/AndroidRuntime(3443): at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
01-15 12:25:01.598: E/AndroidRuntime(3443): at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
01-15 12:25:01.598: E/AndroidRuntime(3443): at
android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
01-15 12:25:01.598: E/AndroidRuntime(3443): at
android.os.Handler.handleCallback(Handler.java:587) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.os.Handler.dispatchMessage(Handler.java:92) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
android.os.Looper.loop(Looper.java:123) 01-15 12:25:01.598:
E/AndroidRuntime(3443): at
android.app.ActivityThread.main(ActivityThread.java:3683) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
java.lang.reflect.Method.invokeNative(Native Method) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
java.lang.reflect.Method.invoke(Method.java:507) 01-15 12:25:01.598:
E/AndroidRuntime(3443): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 12:25:01.598: E/AndroidRuntime(3443): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-15
12:25:01.598: E/AndroidRuntime(3443): at
dalvik.system.NativeStart.main(Native Method)

  • 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-17T08:26:50+00:00Added an answer on June 17, 2026 at 8:26 am

    Figured out solution, in my fragment I have an arraylist which holds a list of objects, whenever I click the button to go to next fragment, or back button to go back, I call remove all views on my relative layout, and now it works fine.

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

Sidebar

Related Questions

I wish to dynamically create and name instances of my object at runtime as
In my app I have a module named client. I wish to create a
I have a simple table and I wish to add rows dynamically to them
I'm having an issue in my application. I wish to dynamically create several tabbed
I have a canvas which I wish to manipulate. I want to use a
Here i create table in database dynamically. User enters name as his wish and
I am currently attempting to create an Inno script installer which requests a list
I have a piece of VB code that dynamically finds my current file path
I wish to create SCORM packages on the fly i.e. programmatically in ASP.NET so
I wish to make the uploaded file contents only viewable on the browser i.e

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.