我尝试使用实现CvCameraViewListener2的片段更改片段,但是当我按导航抽屉中的项目时,应用程序崩溃。
我认为问题出现在onResume()方法中。
这里是我的代码:
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;
}
}
};
这是电话:
Fragment cameraFragment = new CameraFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main, cameraFragment).commit();
布局/ 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>
崩溃日志:
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)