Stack Exchange
Use camera with opencv fragment
I'm trying to change fragment with a fragment that implements CvCameraViewListener2 but when I press the item in the navigation drawer the app crashes.
i thinks the problem is in the onResume() method.
here my code:
CameraFragment.java:
public class CameraFragment extends Fragment implements CvCameraViewListener2
{
    private static final String TAG = "CameraViewFragment";
    private CameraBridgeViewBase mOpenCvCameraView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.surface_view,
                container, false);

        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);

        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_FULLSCREEN);

        getActivity().setContentView(R.layout.surface_view);

        mOpenCvCameraView = (CameraBridgeViewBase) getActivity().findViewById(R.id.surface_view);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);

        return rootView;
    }

    public CameraFragment()
    {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this.getActivity(),
                mLoaderCallback);
    }

    @Override
    public void onCameraViewStarted(int width, int height)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCameraViewStopped()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public Mat onCameraFrame(CvCameraViewFrame inputFrame)
    {
        // TODO Auto-generated method stub
        return inputFrame.rgba();
    }

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this.getActivity())
    {
        @Override
        public void onManagerConnected(int status)
        {
            switch (status)
            {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded Successfully");
                    mOpenCvCameraView.enableView();
                }
                break;
                default:
                {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };

This is the call:
Fragment cameraFragment = new CameraFragment();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_main, cameraFragment).commit();

layout/surface_view.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

</FrameLayout>

crash log:
05-01 14:47:39.360 13052-13052/com.example.buzzo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.buzzo.myapplication, PID: 13052
                                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
                                                                                         at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222)
                                                                                         at android.app.AlertDialog$Builder.<init>(AlertDialog.java:452)
                                                                                         at org.opencv.android.BaseLoaderCallback.onPackageInstall(BaseLoaderCallback.java:90)
                                                                                         at org.opencv.android.AsyncServiceHelper.InstallService(AsyncServiceHelper.java:117)
                                                                                         at org.opencv.android.AsyncServiceHelper.initOpenCV(AsyncServiceHelper.java:33)
                                                                                         at org.opencv.android.OpenCVLoader.initAsync(OpenCVLoader.java:114)
                                                                                         at com.example.buzzo.myapplication.CameraFragment.onResume(CameraFragment.java:75)
                                                                                         at android.app.Fragment.performResume(Fragment.java:2399)
                                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
                                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
                                                                                         at android.app.BackStackRecord.run(BackStackRecord.java:816)
                                                                                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
                                                                                         at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
                                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6121)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)


Can you share the crash log?

yes, you can found it in the question

It seems your context is null even though it shouldn't be. I suggest verifying that this.getActivity() actually returns a non-null value.

i resolved: i moved mLoaderCallback initialization into onCreateView

@MicheleBuzzoni The same is not working for me...As, your say i moved the mloaderCallback to initialization into onCreateView, but still no luck
