I don't understand, why we could use nearly every material in a cavity with a hole to absorb/ emit every wavelength.
In practice, if you wanted to make a cavity that produces radiation close to blackbody radiation, all materials are not equal. The original measurements of equilibrium radiation (Lummer and Kurlbaum, and Lummer and Pringsheim) used heated metallic cavities with complicated inner surface covered with well absorbing "black" materials (metal oxides) to produce and contain the radiation.
The fact one could use any material is true in theory; if thermodynamic equilibrium is somehow present in the cavity, then details of the material inside don't matter, because spectrum of equilibrium radiation depends only on temperature, not on absorption/emission properties of the material.
However, one can ask whether a state close to thermodynamic equilibrium will establish itself spontaneously in cavity with any material. This requires some things: 1) that the cavity is well insulated from leaking radiation, either due to it being well reflecting, or due to it being made of thick, badly conducting material, 2) that the material inside interacts at least a little with radiation of any frequency.
In practice, no cavity is perfectly insulating and all materials are transparent to radiation of high-enough frequency, so perfect equilibrium at all frequencies can't be established.
If the material don't have the exact energy gap g for example, it couldn't absorb a photon
Material needs not have "exact energy gap". When radiation of frequency $f$ interacts with quantum system that has no available transitions of energy $hf$, the system will still be able to change states and get some energy from the radiation. The idea of gaps and transitions is a simplified picture, an approximation, giving the major part of material response. But interaction and energy exchange is not limited to cases where exact matches of $hf$ and energy levels occur.
If we had a perfectly insulating cavity, and the matter inside the cavity (or the material that the cavity is made of) had a very low absorptivity at some frequency (low ratio of absorbed and incoming intensity of radiation), establishing equilibrium radiation at that frequency will take a long time. The lower the absorptivity, the longer it takes. In the limit where the matter does not absorb at all at some frequency, equilibrium will never establish itself at that frequency.
Real bodies have non-zero absorptivity at all frequencies higher than zero; as far as I know, there are no perfectly transparent or perfectly reflecting bodies. So after a long enough time, in a well insulating cavity, radiation could get close to the equilibrium state.