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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:34:27+00:00 2026-05-18T02:34:27+00:00

Creating a layout in Java as the number of TableLayouts required is not known

  • 0

Creating a layout in Java as the number of TableLayouts required is not known as designtime.

I get an IllegalStateException telling me to remove the View (from it’s current parent) before assigning it to another parent, when I call createPlayerTables()

The exception is thrown at the first line in this loop, when I try to add an ImageView from the List of ImageViews to the first TableRow:

for (int i = 0; i < 3; i++) {
    tableRowsLst.get(0).addView((ImageView) imageViewsLst.get(i));
    tableRowsLst.get(1).addView((ImageView) imageViewsLst.get(i+3));
}

The error suggests that the ImageView has already been added to a ViewGroup, but seeing the code below, I create new ImageViews, and I only add them to an ViewGroup at the line that it errors at, so I’m not sure why it’s failing.

// List<ImageView> imageViewsLst = new ...
// List<TableRow> tableRowsLst = new ...

/**
* Initialises the TableLayouts, one per player
*/ 
private TableLayout createPlayerTables(int playerNum) {
    ...

    for (int i = 0; i < 6; i++) {
        imageViewsLst.add(new ImageView(this));
        ...
    }

    for (int i = 0; i < 3; i++) {
        tableRowsLst.add(new TableRow(this));
        ...
    }

    for (int i = 0; i < 3; i++) {
        tableRowsLst.get(0).addView((ImageView) imageViewsLst.get(i));
        tableRowsLst.get(1).addView((ImageView) imageViewsLst.get(i+3));
    }

    ...
}
  • 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-05-18T02:34:28+00:00Added an answer on May 18, 2026 at 2:34 am

    In this loop:

    for (int i = 0; i < 3; i++){
       tableRowsLst.add(new TableRow(this));
       tableRowsLst.get(i).setLayoutParams(
           new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, dipToPixels(55)));
       tableRowsLst.get(i).setOrientation(LinearLayout.HORIZONTAL);
    }
    

    you just keep adding new TableRows to tableRowsLst, but you always only use the first three elements.

    Clear the list before the loop:

    tableRowsLst.clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Button otherthan creating it from xml layout.i want to
I am creating a list view layout and receive an ArrayAdapter warning. The error
Dynamically I am creating the linear layout and adding the views to linear layout
I'm creating a cross platform application using Swing and Netbeans (Group Layout). Is there
So I'm creating columns dynamically for a Grid layout in Silverlight (V3.0, C#): LayoutRoot.ColumnDefinitions.Add(new
I am creating a website that uses a fluid layout with artificial columns. I
I'm creating a game in Java for fun and I'm trying to decide how
First off my background: I'm new to Java coming over from Ruby. If that
I am creating a qr code reader application and I took help from the
Since java does not support multiple inheritance I have a problem getting this to

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.