The focus racking problem means that you're shooting in conditions that the camera is having trouble with. The subject may have low contrast, be too dim, other objects may be in the way, or the selected focus mode may be incorrect for the subject. Some solutions:
If the area is too dim, causing the focus trouble, turn on some lights, use the on-camera focus-assist beam, or add a flash with a focus-assist beam. Simply, it's dark and the camera needs help to see.
Low contrast subjects -- especially obvious and trouble in dark environments -- don't provide anywhere for the AF system to know if focus has been achieved because there's such little difference. Trying to shoot a white wall, for example, is a frustrating exercise because the camera won't know what's in focus. Dark/light edges provide contrast for the AF system to know what it's supposed to focus on.
A wide-area focus mode can be trouble if you're shooting a small subject like a bird behind trees. The camera doesn't know if it should focus on the branches in the foreground or the bird behind them, for example. Switch to a single-point AF and you can be specific about what you want the camera to focus on, and therefore be sure it has a better chance locking on to what you want to shoot.
Using a lens with an aperture of f4 or f5.6 means that the AF system is at the edge of what it can do. A faster lens, such as one with an f2.8 or f1.8 maximum aperture will collect more light for the AF system, and therefore have an easier time focusing.