• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps "The specified child has already a parent"-error

Ojive

Lurker
Heya!

I dont understand why is this such a big issue in Android API (2.3.3).

Im creating a TableLayout, a TableRow and add things to the row, then add the row to the layout. Everything is cool, BUT:

Say for instance i have a TextView view. I add it to one row, and i want to add a copy of that object into my 2nd row.

What i usually do in Java:

ArrayList<String> textArray1 = new ArrayList<String>();
ArrayList<String> textArray2 = new ArrayList<String>();

String test = "Test";

textArray1.add(test);
textArray2.add(test);

Apparently, you cannot do this in Android API. When adding "test" to textArray2, you would get an exception saying "The specified child has already a parent".

Why cant i add a duplicate object to different rows? Say i have a label(TextView) saying "Income" and i want to use it as a name of a column in my table. It would be stupid to write 3-4-5 lines of same code, to add the same label "Income" for each row in the table.

What am i missing?
 
The thing you're missing is the difference between duplicating an object and a view.

A view is a very important type of object to the Android internal framework. It's not something that can exist in two places at once. Each view is it's own object.

Contrast to that, your example would work fine, more than one array can hold a reference to your text String.

So you need to think about it a bit differently. For instance, you could copy the text String object instead of a View object.

hope that helps.
 
The thing you're missing is the difference between duplicating an object and a view.

A view is a very important type of object to the Android internal framework. It's not something that can exist in two places at once. Each view is it's own object.

Contrast to that, your example would work fine, more than one array can hold a reference to your text String.

So you need to think about it a bit differently. For instance, you could copy the text String object instead of a View object.

hope that helps.

It helps, but not quite does it :D

I see a TextView the same way as i see JLabel in Java, perhaps thats what im doing wrong? What would an android equivalent be for JLabel and JTextField?

My general idea is this:

------------Heading Labeltext #1-------------
Text | Text | Text | Text

------------Heading Labeltext #2-------------
Text | Text | Text | Text

In Java, i would simply use JLabel for everything, in a GridBagLayout layout manager. JLabel would be the most important thing in this example though. If i wanted a text input field, i'd use JTextfield.

Am i being any way clear on this? :D It's monday, and it seems that im having brainfarts here hehe (not enough coffee!)
 
It helps, but not quite does it :D

I see a TextView the same way as i see JLabel in Java, perhaps thats what im doing wrong?
Yes, this is incorrect. :)

What would an android equivalent be for JLabel and JTextField?

Nothing unfortunately... :(

The fact that you can duplicate a view object like JLabel is *probably* more abnormal from my programming experiences. The norm would be the opposite.

Views are generally not something you copy... In a layout framework, when you pass a view object to the framework, it will do the layout for you. Android is taking the View you are passing it and attempting to lay it out, but it is noticing that the view has already been laid out on the screen. Hence the error" this object already has a parent." Basically Android is warning you that you are trying to put a single thing in two places, and that single thing can only exist in one place at a time.


Imagine if it also made a deep object copy of the view you passed it.... That would cause performance problems and data staleness. Basically what would happen if you called setText on the view? Would it change all views? Would it only change the most recent copy of the view?


(Maybe the Jlabel isnt actually a view but rather a data object (or proxy) that is used to create a view?)

If you really want to copy an object you could make a deep copy yourself, but I think it's easier to just make more views -- say, in a quick for-loop.


hope that helps :)


Also I'm pretty sure there is an easy way to do what you are after, but I dont use table/grid layout much so I would need to look it up. I will have a look later today :)
 
Yes, this is incorrect. :)



Nothing unfortunately... :(

The fact that you can duplicate a view object like JLabel is *probably* more abnormal from my programming experiences. The norm would be the opposite.

Views are generally not something you copy... In a layout framework, when you pass a view object to the framework, it will do the layout for you. Android is taking the View you are passing it and attempting to lay it out, but it is noticing that the view has already been laid out on the screen. Hence the error" this object already has a parent." Basically Android is warning you that you are trying to put a single thing in two places, and that single thing can only exist in one place at a time.


Imagine if it also made a deep object copy of the view you passed it.... That would cause performance problems and data staleness. Basically what would happen if you called setText on the view? Would it change all views? Would it only change the most recent copy of the view?


(Maybe the Jlabel isnt actually a view but rather a data object (or proxy) that is used to create a view?)

If you really want to copy an object you could make a deep copy yourself, but I think it's easier to just make more views -- say, in a quick for-loop.


hope that helps :)


Also I'm pretty sure there is an easy way to do what you are after, but I dont use table/grid layout much so I would need to look it up. I will have a look later today :)

Thanks a bunch for taking the time to explain, i think i understand now. It does feel however that Android SDK is not quite done yet :p Granted Java SDK had YEARS of development, i feel that Android SDK is lacking lots of the things Java SDK have, that makes a coder's life so much easier hehe

What im trying to do (behold my mad MSPaint skills):
appv.jpg


As you can see, model, engine and torque could be reused as a single object that is inserted everywhere. I now see that this is impossible. It is so clumsy to have to make "model1, model2, model3" just because you need 3x of that text :p

Entry will be fields parsed from a JSON object, i am using a PHP script to make a query. I am then sorting those result rows according to the types "Cars", "Bycycles", "Planes" (it was just an example anyhow).

Looking at the example, the first thing that hits me is some sort of Table with columns and rows. Perhaps there is a much easier and cleaner way to do this?
 
There are definitely ways of binding objects to views as data providers...

I just dont know what the right ones are for grid/table views
 
Back
Top Bottom