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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:18:30+00:00 2026-06-09T04:18:30+00:00

I am trying to programmatically add a Fragment to a ScrollView that has a

  • 0

I am trying to programmatically add a Fragment to a ScrollView that has a LinearLayout. For some reason, when I call the following function, it doesn’t appear to add any element to the LinearLayout.

Here is the code:
When the user clicks a button the following code is called that is supposed to add the fragment to the LinearLayout

public void refresh(View v) {
    CourseManagement cm = CourseManagement.getInstance();
    ArrayList<Course> courses = cm.getCourses();
    if(courses.size()>0) {
        Iterator<Course> it = courses.iterator();
        while(it.hasNext()) {
            Course c = it.next();
            addCourse(c);
        }
    } else {
        //No Classes In List;
    }
}

This function calls the following function for every class in the arraylist:

private void addCourse(Course c) {
    LinearLayout destination = (LinearLayout) findViewById(R.id.addListCourses);
    FrameLayout fl = new FrameLayout(this);
    CreateFragTests frag = new CreateFragTests();
    fl.setId(frag.getId());
    fl.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(fl.getId(), frag).commit();
    destination.addView(fl);
    //frag.setCourse(c);
}

The Fragment itself is here:

public class CreateFragTests extends Fragment {

private static int uniqID = 0;
private static String uniqPrefix = "courseList";
private Course course;

public CreateFragTests() {
    super();
    uniqID++;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //After some debugging, I have found that container is passed as null to this
    //this function which may be part of the problem?
    return inflater.inflate(R.layout.activity_create_frag_tests, container, true);
}

public void setCourse(Course c) {
    this.course = c;
    setCourseName(course.name);
    setInstructorsName(course.instructorLastName+", "+course.instructorFirstName);
}

public String getUniqID() {
    return uniqPrefix+"_"+uniqID;
}
}

After some debugging, I have found that when the onCreateView() is called, it receivies a null value for container. I have modeled my code after the examples given here: How do I add a Fragment to an Activity with a programmatically created content view and here: http://developer.android.com/training/basics/fragments/fragment-ui.html

EDIT: Also if I use this same code but try adding a textview instead of a fragment it works just fine.

  • 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-09T04:18:32+00:00Added an answer on June 9, 2026 at 4:18 am

    The problem lies here fl.setId(frag.getId()); . Instead of passing frag.getId() as its id, you should pass a unique id into it.

    There are 2 ways you can do, either define the id in XML or in the class
    For example

    private static final int CONTAINER_ID = 123456;

    And use it to set the FrameLayout id.

    fl.setId(CONTAINER_ID);

    or a simpler approach will be as follow

    private void addCourse(Course c) {
        CreateFragTests frag = new CreateFragTests();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.addListCourses, frag).commit();
        //frag.setCourse(c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a fragment programmatically to a LinearLayout, but the fragment
I'm trying to add a horizontal LinearLayout programmatically, making sure the second child takes
I am trying to add another subview programmatically based on some event (user taps
I am trying to programmatically add the following Target to my Visual Studio Project
I have an access table that I am trying to add fields programmatically using
I am trying to add rows in a TableLayout programmatically and I am following
Trying to programmatically add a splash image that hangs around for a specified amount
I'm trying to add table rows programmatically following the code here /* Find Tablelayout
So, I am trying to programmatically add in a ModelFilter to my ObjectListView that
Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.

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.