I'm writing an app and i've decided to use fragments in its design due to flexibility, but I'm getting nullobjectreference when I run the app. The problem is that fragment is not waiting for user to give permission, because it's running asynchronously so when it's creating camera instance it gets null instead due to lack of permission even though I check them first. Here's the code for onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(checkCameraHardware(this.getActivity())){
checkPermissions();
}else {
try {
this.finalize();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
LinearLayout buttonLayout = (LinearLayout)setButton();
mFrameLayout = new FrameLayout(this.getActivity());
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
frameParams.gravity = Gravity.BOTTOM;
mFrameLayout.setLayoutParams(frameParams);
mFrameLayout.addView(mPreview);
mFrameLayout.addView(buttonLayout);
return mFrameLayout;
}
and for when i create the camera instance:
private void cameraCreator(){
mCamera = getCameraInstance();
mCamera.setDisplayOrientation(90);
mPreview = new CameraPreview(this.getActivity(), mCamera);
}
I know that I can't suspend the thread just for permission request, but what can I do?