findViewByID returns null

Possibly, you are calling findViewById before calling setContentView? If that's the case, try calling findViewById AFTER calling setContentView


FindViewById can be null if you call the wrong super constructor in a custom view. The ID tag is part of attrs, so if you ignore attrs, you delete the ID.

This would be wrong

public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context);
}

This is correct

public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context,attrs);
}

Make sure you don't have multiple versions of your layout for different screen densities. I ran into this problem once when adding a new id to an existing layout but forgot to update the hdpi version. If you forget to update all versions of the layout file it will work for some screen densities but not others.


which returns null

Possibly because you are calling it too early. Wait until onFinishInflate(). Here is a sample project demonstrating a custom View accessing its contents.