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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:48:38+00:00 2026-05-20T01:48:38+00:00

I’m new to android and programming in general so go easy! I have created

  • 0

I’m new to android and programming in general so go easy!

I have created a – probably overcomplicated but it does what I want – main.xml with my layout which goes something like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_scrollview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<LinearLayout
    ...
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/title"
        android:gravity="center"
    />
    <TableLayout
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:stretchColumns="0"
        >
        <TableRow>
            <TextView/>
            <Spinner/>
        </TableRow>
        <TableRow>
            <TextView/>
            <Spinner/>
        </TableRow>
    </TableLayout>
    <TableLayout
        android:id="@+id/main_tablelayout2"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:stretchColumns="*"
        >
        <include
            android:id="@+id/include"
            layout="@layout/price0"
        />
        <TableRow>
            <TextView/>
            <EditText/>
        </TableRow>
    </TableLayout>
</LinearLayout>
</ScrollView>

This is one of the options that I’m trying to include (price1.xml):

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/price1"
        android:gravity="center_vertical"
    />
    <EditText
        android:id="@+id/price1"
    />
</TableRow> 
</merge>

I am trying to dynamically change the include depending on what value one of the spinners has, but I get a force close on starting the app.

The logcat error is:

01-18 15:31:42.826: ERROR/AndroidRuntime(719): FATAL EXCEPTION: main
01-18 15:31:42.826: ERROR/AndroidRuntime(719): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.TableLayout.addView(TableLayout.java:421)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.TableLayout.addView(TableLayout.java:403)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.TableLayout.addView(TableLayout.java:394)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at com.kavi.eta.Calculate$1.onItemSelected(Calculate.java:36)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.AdapterView.access$200(AdapterView.java:42)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.os.Handler.handleCallback(Handler.java:587)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.os.Looper.loop(Looper.java:123)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.app.ActivityThread.main(ActivityThread.java:3647)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at java.lang.reflect.Method.invoke(Method.java:507)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at dalvik.system.NativeStart.main(Native Method)

Looks like I’m doing the java code wrong but I can’t figure out how to properly remove the default include and replace it with another layout:

public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { 
            int index = formatSpinner.getSelectedItemPosition();
            if (index == 0) {

                TableLayout tl = (TableLayout)findViewById(R.id.main_tablelayout2);
                tl.removeView(findViewById(R.id.include));

                LayoutInflater inflater = getLayoutInflater();
                tl.addView(inflater.inflate(R.layout.price1, tl, true));
            }

Sorry about the long post but I’ve searched everywhere and can’t find a clear answer. Hopefully this’ll at least provide a decent-ish example…

  • 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-20T01:48:39+00:00Added an answer on May 20, 2026 at 1:48 am

    Didn’t really find a proper solution to this, but for completeness, this is what I ended up doing:
    I kept getting a NullPointerException when using findViewById for any of the views in the price1.xml (ie The xml being included). Is this by design?
    This was a problem as I had to dynamically add/remove certain views depending on the user input.

    I still have an include tag in my main.xml, which I remove by looking up the position relative to it’s parent (again, I couldn’t get the findViewById to work but this just as effetive). I then inflate another xml file in it’s place, depending on the user input.

    if (position == 0) {
                        LinearLayout ll = (LinearLayout)findViewById(R.id.main_linearlayout);
                        ll.removeViewAt(3);
                        LayoutInflater inflater = getLayoutInflater();
                        ll.addView(inflater.inflate(R.layout.replacement1, null), 3);
    
                    } else if (position == 1) {
                        LinearLayout ll = (LinearLayout)findViewById(R.id.main_linearlayout);
                        ll.removeViewAt(3);
                        LayoutInflater inflater = getLayoutInflater();
                        ll.addView(inflater.inflate(R.layout.replacement2, null), 3);
    

    I have also cleaned up my main.xml to remove unnecessary nesting of layouts and ditched the merge tags and used TableLayout as the root tags for my included xml.
    That seems to be my UI troubles sorted (for now!)…

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

Sidebar

Related Questions

No related questions found

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.