-1

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?

  • don't start the thread in `onCreate()` but in `onRequestPermissionsResult` – Shark Jun 14 '17 at 12:39
  • you mean in FragmentActivity that called the fragment? – Karol Wojtulewicz Jun 14 '17 at 13:14
  • I wouldn't know because you posted code that doesn't really have much relevance to the problem you're describing in your question. The `cameraCreator()` isn't being called in your `onCreateView()` snippet, so like I said, i wouldn't really know. But yes, probably in the `Activity`- that-instantiated-your-fragment's `onRequestPermissionResult()` – Shark Jun 14 '17 at 13:24

1 Answers1

-1

Add fragment to your Activity at Runtime and ask for permission before doing so

sasan
  • 111
  • 11