To be honest, I always worked with xml layouts when it came to Android apps, so I never used these methods myself. And, seeing as I don't have any code snippet of yours, I can only guess the problem.
Android doesn't assign ids automatically. If you add a View directly in Java (or in XML without specifying the android:id attribute), that View has no id, and getId() returns View.NO_ID (which has a value of -1, if you want to check). That means, before you use getId() for the first time, you have to set an id (my guess is that is has to be positive (i.e. >= 0) to be a valid id, but like I said, I'm not too sure).
If that information didn't help, be so kind as to post the relevant piece of code.
A la fin, il y aura seulement de la beauté.