首页 \ 问答 \ 从Stopped返回时,Android Simple Camera Control应用程序崩溃(Android Simple Camera Control App crashes when returning from Stopped)

从Stopped返回时,Android Simple Camera Control应用程序崩溃(Android Simple Camera Control App crashes when returning from Stopped)

我正在通过developer.Android教程构建相机应用程序,但我总是遇到以下问题:当我的应用程序被放入后台时,即根据活动生命周期停止,我尝试返回它崩溃,告诉我已经停止了工作。

我搜索了其他问题,但他们似乎都没有同样的问题,或者我无法识别它。

我试图找出发生此错误的最小代码量。 (仅限)活动:

public class CameraActivity extends ActionBarActivity {

private Camera mCamera;
private CameraPreview mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_layout);

    // if device hardware has a camera
    if (checkCameraHardware(this))
        // Create an instance of Camera
        mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);


}

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
public void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
}

private void releaseCamera(){
    if (mCamera != null){
        mCamera.stopPreview();      // stop the preview
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}

@Override
public void onResume() {
    super.onResume();

    if (mCamera == null)
        mCamera = getCameraInstance();
}


}

使用此类实例化CameraPreview:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {

         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();

    } catch (IOException e) {
        Log.d("PandaHero", "Error setting camera preview: " + e.getMessage());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // .. same as in the example of the link
}
}

至于错误消息,它总是告诉我在release()之后调用surfaceCreated()。 这让我觉得我必须错过一些重要的相机重新实现或预览某处。 我试图停止预览并在surfaceDestroyed(),onStop()中释放相机,并重新打开onStart()内部的预览,但错误仍然存​​在。 此外,如果我不首先包含预览,则不会发生崩溃(但我的用户也无法看到它,所以这是没有选择)。

所以,是的,我认为在利用生命周期方面,我在这里缺少一些重要的东西。

编辑:我提到的错误消息。

03-02 14:33:45.553  15462-15462/com.example.vrevru.viewlate_camerasetupsimple E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Method called after release()
        at android.hardware.Camera.setPreviewDisplay(Native Method)
        at android.hardware.Camera.setPreviewDisplay(Camera.java:428)
        at com.example.thorsten.viewlate_camerasetupsimple.CameraPreview.surfaceCreated(CameraPreview.java:32)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:639)
        at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:262)
        at android.view.View.dispatchWindowVisibilityChanged(View.java:7440)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1237)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4236)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

I was working through the developer.Android Tutorials on building a Camera App but I always encounter the following problem: When my app is put into the background, i.e. Stopped according to the activity lifecycle, and I try to return to it it crashes, telling me the App has stopped working.

I searched other questions, but none of them seemed to have this same problem or I wasn't able to recognise it.

I tried to figure out the minimum amount of code for this error to occur. The (only) Activity:

public class CameraActivity extends ActionBarActivity {

private Camera mCamera;
private CameraPreview mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_layout);

    // if device hardware has a camera
    if (checkCameraHardware(this))
        // Create an instance of Camera
        mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);


}

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
public void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
}

private void releaseCamera(){
    if (mCamera != null){
        mCamera.stopPreview();      // stop the preview
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}

@Override
public void onResume() {
    super.onResume();

    if (mCamera == null)
        mCamera = getCameraInstance();
}


}

The CameraPreview is instantiated using this class:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {

         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();

    } catch (IOException e) {
        Log.d("PandaHero", "Error setting camera preview: " + e.getMessage());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // .. same as in the example of the link
}
}

As for error messages, it always shows me that surfaceCreated() is called after release(). This made me think that I have to be missing some important reinstantiation of the camera or preview somewhere. I tried to stop the preview and release the camera within surfaceDestroyed(), onStop() and also to reopen the preview inside onStart() but the error persisted. Also, no crashes occur if I do not include the Preview in the first place (but then my users wouldn't be able to see it either, so that's no option).

So yeah, I think there is something vital I am missing here in terms of making use of the life cycle.

Edit: The Error message I mentioned.

03-02 14:33:45.553  15462-15462/com.example.vrevru.viewlate_camerasetupsimple E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Method called after release()
        at android.hardware.Camera.setPreviewDisplay(Native Method)
        at android.hardware.Camera.setPreviewDisplay(Camera.java:428)
        at com.example.thorsten.viewlate_camerasetupsimple.CameraPreview.surfaceCreated(CameraPreview.java:32)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:639)
        at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:262)
        at android.view.View.dispatchWindowVisibilityChanged(View.java:7440)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1237)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4236)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

原文:https://stackoverflow.com/questions/28812006
更新时间:2022-04-22 11:04

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)