title: CWAC-camera exception "Method called after release()" after exiting
body: <p>Firstly, I'm crap at English.</p>
  <p>I'm working on a project which uses Android camera.</p>
  <p>My team is using CWAC-camera, so does my project.</p>
  <p>So far I think it's really good. I'm displaying the preview of my camera.
  The thing is, when I try to close the application (with the physical &quot;return&quot; button), it crashes. Not always.</p>
  <p>In fact it seems it's crashing when I'm closing BEFORE the src.release() in CameraPreviewFragment.
  However, if I close the app after the src.release(), it seems it's working well.
  It's not 100% sure, I said that thanks to the loggers.</p>
  <p>Here is the error :</p>
  <blockquote>
  <p>5529-5529/net.personal.opencv_tester W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41a222a0)</p>
  <p>06-10 10:04:50.960  30241-30241/net.personal.opencv_tester E/MINE DEBUG﹕ Beginning</p>
  <p>06-10 10:04:51.015  30241-30241/net.personal.opencv_tester E/MINE DEBUG﹕ Just Before Release</p>
  <p>06-10 10:04:51.015  30241-30241/net.personal.opencv_tester E/MINE DEBUG﹕ Just After Release</p>
  <p>06-10 10:04:51.015  30241-30241/net.personal.opencv_tester E/MINE DEBUG﹕ End</p>
  <p>06-10 10:04:51.555  30241-30241/net.personal.opencv_tester E/AndroidRuntime﹕</p>
  <p>FATAL EXCEPTION: main</p>
  </blockquote>
  <pre><code>&gt; java.lang.RuntimeException: Method called after release()
          at android.hardware.Camera.setHasPreviewCallback(Native Method)
          at android.hardware.Camera.access$600(Camera.java:139)
          at android.hardware.Camera$EventHandler.handleMessage(Camera.java:867)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:176)
          at android.app.ActivityThread.main(ActivityThread.java:5419)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:525)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  <p>And here is when it works:</p>
  <blockquote>
  <p>06-10 10:06:56.480  32508-32508/net.personal.opencv_tester E/MINE DEBUG﹕ Beginning</p>
  <p>06-10 10:06:56.495  32508-32508/net.personal.opencv_tester E/MINE DEBUG﹕ Just Before Release</p>
  <p>06-10 10:06:56.495  32508-32508/net.personal.opencv_tester E/MINE DEBUG﹕ Just After Release</p>
  <p>06-10 10:06:56.495  32508-32508/net.personal.opencv_tester E/MINE DEBUG﹕ End</p>
  <p>06-10 10:06:57.225  32508-32508/net.personal.opencv_tester E/MINE DEBUG﹕ On detach</p>
  </blockquote>
  <p>Here's my code :</p>
  <p><strong>MainActivity.java</strong></p>
  <pre><code>package net.personal.opencv_tester;
  
  import org.opencv.android.OpenCVLoader;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.KeyEvent;
  import android.view.Window;
  import android.view.WindowManager;
  import com.commonsware.cwac.camera.CameraHost;
  import com.commonsware.cwac.camera.CameraHostProvider;
  import com.commonsware.cwac.camera.SimpleCameraHost;
  
  public class MainActivity
      extends Activity
      implements CameraHostProvider
  {
  
  private static final String TAG = MainActivity.class.getSimpleName();
  
  @Override
  protected void onCreate( Bundle savedInstanceState )
  {
      super.onCreate( savedInstanceState );
  
      requestWindowFeature( Window.FEATURE_NO_TITLE );
      getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
  
      setContentView( R.layout.activity_main );
  }
  
  static {
      if (!OpenCVLoader.initDebug()) {
          // Handle initialization error
      }
  }
  
  @Override
  public CameraHost getCameraHost()
  {
      SimpleCameraHost.Builder builder =
              new SimpleCameraHost.Builder( new CustomCameraHost( this ) );
      builder.useFullBleedPreview( true );
      return builder.build();
  }
  }
  </code></pre>
  <p><strong>CustomCameraHost.java</strong></p>
  <pre><code>package net.personal.opencv_tester;
  
  import java.io.File;
  import android.content.Context;
  import com.commonsware.cwac.camera.SimpleCameraHost;
  
  public class CustomCameraHost
      extends SimpleCameraHost
  {
  
  private final File outFile;
  
  public CustomCameraHost( File outFile, Context context )
  {
      super( context );
      this.outFile = outFile;
  }
  
  public CustomCameraHost( Context context )
  {
      super( context );
      this.outFile = new File(context.getFilesDir(), &quot;test.jpg&quot;);
  }
  
  @Override
  public boolean useSingleShotMode()
  {
      return true;
  }
  
  @Override
  protected File getPhotoPath()
  {
      return outFile;
  }
  
  @Override
  protected boolean scanSavedImage()
  {
      return false;
  }
  }
  </code></pre>
  <p>And finally, where the bug is situated:</p>
  <p><strong>CameraPreviewFragment.java</strong></p>
  <pre><code>package net.personal.opencv_tester;
  
  import org.opencv.core.Core;
  [...] 
  import com.commonsware.cwac.camera.CameraFragment;
  import com.commonsware.cwac.camera.CameraView;
  import com.jjoe64.graphview.BarGraphView;
  import com.jjoe64.graphview.GraphView;
  
  
  public class CameraPreviewFragment
      extends CameraFragment
      implements Camera.PreviewCallback, SensorEventListener, OnClickListener
  {
  
  private CameraView mCameraView;
  private TextView mTextView;
  private GraphView mGraphView;
  private TextView mInfosTextView;
  
  private long mLastCheck = System.currentTimeMillis();
  
  @Override
  public void onAttach( Activity activity )
  {
      Log.e(&quot;MINE DEBUG&quot;, &quot;On attach&quot;);
      super.onAttach( activity );
  }
  
  @Override
  public void onDetach()
  {
      Log.e(&quot;MINE DEBUG&quot;, &quot;On detach&quot;);
      super.onDetach();
  }
  
  @Override
  public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
  {
      View view = LayoutInflater.from( getActivity() ).inflate( R.layout.fragment_preview, container, false );
      view.setOnClickListener( this );
  
      mTextView = ( TextView ) view.findViewById( R.id.cameraWarningTextView );
  
      mCameraView = ( CameraView ) view.findViewById( R.id.cameraSurfaceView );
      mCameraView.setHost( getHost() );
  
      //Camera.open(getHost().getCameraId()).setOneShotPreviewCallback(this);
      mCameraView.setPreviewCallback( this );
      setCameraView( mCameraView );
  
      FrameLayout graphLayout = ( FrameLayout ) view.findViewById( R.id.cameraHistLayout );
      mGraphView = new BarGraphView( getActivity(), &quot;Histogram&quot; );
      mGraphView.getGraphViewStyle().setTextSize( 17f );
      mGraphView.getGraphViewStyle().setHorizontalLabelsColor( 0xffffffff );
      mGraphView.getGraphViewStyle().setVerticalLabelsColor( 0xffffffff );
      mGraphView.setVisibility( View.GONE );
      graphLayout.addView( mGraphView );
  
      mInfosTextView = ( TextView ) view.findViewById( R.id.cameraInfosTextView );
      mInfosTextView.setVisibility( View.GONE );
  
      return view;
  }
  
  @Override
  public void onClick( View v )
  {
      autoFocus();
  }
  
  @Override
  public void onSensorChanged( SensorEvent event )
  {   
  }
  
  @Override
  public void onAccuracyChanged( Sensor sensor, int accuracy )
  {
  }
  
  @Override
  public void onPreviewFrame( byte[] data, Camera camera )
  {
      Log.e(&quot;MINE DEBUG&quot;, &quot;Begining&quot;);
      long now = System.currentTimeMillis();
  
      // Check an image every 500ms
      if (mLastCheck + 500 &gt; now) {
          return;
      }
      mLastCheck = now;
      
      int frameHeight = camera.getParameters().getPreviewSize().height;
      int frameWidth = camera.getParameters().getPreviewSize().width;
  
      // Create default grayscale matrix
  
      Mat src = new Mat( frameHeight, frameWidth, CvType.CV_8UC1 );
      src.put( 0, 0, data );
  
      // Resize if image is too large (for performances)
      if (src.width() &gt; 640) {
          double ratio = ( double ) src.width() / ( double ) src.height();
          Imgproc.resize( src, src, new Size( 640, 640 / ratio ), 0, 0, Imgproc.INTER_LINEAR );
      }
  
      // Remove noise
      Imgproc.GaussianBlur( src, src, new Size( 3, 3 ), 0.5 );
  
      try {
          mTextView.setText( &quot;Valid image&quot; );
          mTextView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ic_capture_ok), null,
                  null, null);
      } finally {
          Log.e(&quot;CHECK DEBUG&quot;, &quot;Just Before Release&quot;);
          src.release();
          Log.e(&quot;CHECK DEBUG&quot;, &quot;Just After Release&quot;);
      }
      Log.e(&quot;MINE DEBUG&quot;, &quot;End&quot;);
  }
  }
  </code></pre>
  <p>I browed StackOverflow and found a &quot;fix&quot;, but it isn't working</p>
  <pre><code> mCameraView.setPreviewCallback(null);
  </code></pre>
  <p>Thank you very much for your patience. I can't find the fix.</p>