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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:09:03+00:00 2026-06-03T13:09:03+00:00

bit of a rough time getting this to work and its an understanding piece

  • 0

bit of a rough time getting this to work and its an understanding piece on my end…

If i have a spinner item that contains the text “The [item] is [color]” and upon selecting this, I would like it to populate a… tablerow or something like that(or just a relativelayout)… with buttons and there would be two buttons, [item] and [color], stacked one on the other.

public void onItemSelected(AdapterView<?> parentview, View arg1, int position, long id) 
{

    final TableLayout t1 = (TableLayout)findViewById(R.id.button_items);
    final TableRow tr = new TableRow(t1.getContext());
ArrayList<String> words = Create_ArrayList(R.raw.titles);  

// Create_ArrayList just parses known words like the [item] and [color] items and puts them into an array… for enumeration purposes later on.

String sentence = (String) spin.getSelectedItem();

    if(sentence.contains("[item]"))
    {
        String line = words.get(1);
        ArrayList<String> x = getParts(line);  

//arraylist should just be [item] and [color] at this point…

        Toast.makeText(getBaseContext(), Integer.toString(x.size()), Toast.LENGTH_SHORT).show();
    for(int i = 0; i<x.size(); i++)
    {
              Toast.makeText(getBaseContext(), x.get(i), Toast.LENGTH_LONG).show();

            Button btn = new Button(tr.getContext());
       btn.setText(x.get(i));
       btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
       tr.addView(btn);
       t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    }   
}
}

but i keep getting…

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

and the buttons dont show up… app just crashes… into a mountain.

help is much appreciated… Thanks all!

  • 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-03T13:09:04+00:00Added an answer on June 3, 2026 at 1:09 pm

    In the first code block

    final TableLayout t1 = (TableLayout)findViewById(R.id.button_items);
    

    you already have the view of t1, but again in the third code block you are adding the view

    tr.addView(btn);
           t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    

    that is the reason for your exception.

    When a View is allready used (e.g., you got it with findViewById, don’t use addView on it). When you want to add a view, use addView with a NEW view. You can add several of these new views to one view, but you cannot add that one view multiple times.

    This is what i found in some other post

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

Sidebar

Related Questions

Bit support question. Apologies for that. I have an application linked with GNU readline.
In C# I have an intrusive tree structure that looks like this: public abstract
I'm having a bit of a rough time laying out how I would count
My knowledge of C++ arcana is a bit rough. Let's say I have the
I'm currently having a bit of a rough time uploading an image to my
Bit of a puzzle for you.... I have a 730px wide, auto height div.
Bit of an unusual question, but I have setup a field inside a MySQL
Bit of a javascript newbie so not sure if this question is easy or
Bit of a philosophical question. If you have a public website with a site
I have an issue that I feel many programmers can relate to... I have

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.