tags:

views:

1412

answers:

3

I need to create view without XML. I retrieve the id by the getID method of the views but I get a ResourceNotFoundException. I tried to use to fix this ID the setID method but it doesn't work anymore.

+1  A: 

If you haven't declared your view and put id on it in layout XML file, you can't retrieve it with

findViewById(int)

If you create your view without XML, you should manage it without resource id. You just create the view and name it, then use the name to operate with it.

You can always pass reference of it to other classes, or make it accessible through the class that created it.

Still, you would you want to NOT declare it in XML? Usually, when you start a project, you can't see how you can do something with XML layout, and choose to create most views programmatically. After you move forward with the project, or gather more experience, in pretty much all the cases, you see you could have done it with XML.

Dimitar Dimitrov
Unfortunately, I can't use XML layout... implementation reasons...My problem is that I want to use an ListView populated with an ArrayAdapater ant the ArrayAdapter constructor needs a view ID !
Arutha
Well, in that case, use the setId(int) method of the View class.
Dimitar Dimitrov
It's what I did, but I get a ResourceNotFoundException at runtime
Arutha
Have a look here: http://groups.google.com/group/android-developers/browse_thread/thread/222fda4be2452100?fwc=2
Dimitar Dimitrov
A: 

If you try to print the getId(int) of the View are you getting a valid id?

Try to use the findViewById(int) in the correct context e.g. yourContext.findViewById(int);

MrThys
yes, i obtain the id that i specified in the setID method but the findViewByID return an exception (ResourceNotFound)
Arutha
Try to use yourActivity.findViewById(int); or yourActivity.getBaseContext().findViewById to find the view.If that doesn't work I think you have to make a reference to the created View somewhere so that you have the view at hand immediatly
MrThys
A: 

I think the problem is following. When you call findViewById() it searches for that view in the display tree. So to get the view from the tree you should have already added your view to the tree by using function addView(). If you have not done this it may not be able to find the view in the tree.

srirama