I have a custom view that is extended from RelativeLayout.
I wanted to try to make this custom view as a singleton.
I have this code:
private static CameraView sCameraView;
public static synchronized CameraView getInstance(Context context) {
if (sCameraView == null) {
sCameraView = new CameraView(context);
}
return sCameraView;
}
And tried to use this from a fragment as:
private CameraView mCameraView = CameraView.getInstance(getActivity().getApplicationContext());
mCameraView = (CameraView) mView.findViewById(R.id.camera_preview);
I'm getting a NullPointException from this code. Then also an InflateException when I switch things around a little bit.
How would I approach to access the static instance from the custom view and assign it to a variable? because doing this:
CameraView.getInstance(getActivity().getApplicationContext()) = (CameraView) mView.findViewById(R.id.camera_preview);
isn't allowed.
Any help will be very appreciated.