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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:45:08+00:00 2026-05-27T13:45:08+00:00

I am trying to insert rows from my code in a TableLayout. I got

  • 0

I am trying to insert rows from my code in a TableLayout. I got several tutorials on internet and stackOverflow and some how each time i get this exception.

12-12 17:54:07.027: E/AndroidRuntime(1295): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

12-12 17:54:07.027: E/AndroidRuntime(1295):     at com.kaushik.TestActivity.onCreate(TestActivity.java:41)

Here is the activityclass:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Find Tablelayout defined in main.xml */
        TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);
        /* Create a new row to be added. */
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        /* Create a Button to be the row-content. */
        Button b = new Button(this);
        b.setText("Dynamic Button");
        b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        /* Add Button to row. */
        tr.addView(b);
        /* Add row to TableLayout. */
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        /* adding another row */
        TableRow tr2 = new TableRow(this);
        tr2.addView(b); // Exception is here
        tl.addView(tr2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }

and here is the XML

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myTableLayout"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
</TableLayout>

Please help me.

  • 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-27T13:45:09+00:00Added an answer on May 27, 2026 at 1:45 pm

    Your are doing wrong with

     /* adding another row */
     TableRow tr2 = new TableRow(this);
     tr2.addView(b); // Exception is here
    

    B is a button , as its already added in your table 1st row “t1”. As button is a view and each view can be hold by one parent only. The Button b is already shown to 1st row. Its can be show again on row2.

    As it don’t make a logic when user click the button or row1 or row2 then how to know which button is pressed? I mean you can’t know it is pressed by row 1 or row 2. So this is unexpected thing you are doing.

    As in

    onClick(View view){
       if(view == b){
           // So you cant do that this is button row1 button or row2 button.
       }
    
       // Or you can check the pressed button by id which will also be same. 
    
    }
    

    So you should create new Button button2 and then add to row2.

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

Sidebar

Related Questions

I am trying to insert a time only value, but get the following error
I am trying to insert rows into a MySQL database from an Access database
So I am trying to insert multiples rows of data from one table to
I'm trying to build a safe user authentication system. The code is from http://net.tutsplus.com/tutorials/php/simple-techniques-to-lock-down-your-website/
So I am trying to Delete some items from the database before I Insert
I am trying to insert thousands of rows into a table and performance is
Trying to insert 315K Gif files into an Oracle 10g database. Everytime I get
I am trying to INSERT INTO a table using the input from another table.
I'm trying to insert some import lines into a python source file, but i
I have a problem when trying to delete rows from my UITableView: The UITableView

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.