重启然后pressing按钮

重启然后pressing按钮

本文介绍了应用程序崩溃时,关闭/重启然后pressing按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行Eclipse和诚实自学的Andr​​oid编程的基础知识。我想我会从一个简单的手电筒的应用程序,这样我可以得到一个更好地了解如何工作的。该应用程序本身工​​作正常,并启动相机闪光灯。

我的问题是,当我preSS主页按钮,然后返回到应用程序和preSS按钮,打开手电筒,应用程序崩溃。我一直在阅读了很多,我猜我没有一个onResume这是因为,但我可能是错的。任何帮助是极大的AP preciated。

 进口android.app.Activity;
  进口android.content.Context;
  进口android.content.pm.PackageManager;
  进口android.hardware.Camera;
  进口android.hardware.Camera.Parameters;
  进口android.os.Bundle;
  进口android.util.Log;
  进口android.view.View;
  进口android.view.View.OnClickListener;
  进口android.widget.Button;
  进口com.pmm.lettherebe.R;    公共类MainActivity延伸活动{//标志检测闪光灯开启或关闭
私人布尔isLighOn = FALSE;私人相机拍照;私人Button按钮;@覆盖
保护无效的onPause(){
    super.onPause();    如果(相机!= NULL){
        camera.release();;
    }
}
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    按钮=(按钮)findViewById(R.id.buttonFlashlight);    上下文的背景下=这;
    软件包管理系统下午= context.getPackageManager();    //如果设备支持相机?
    如果(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        Log.e(犯错,设备没有摄像头!);
        返回;
    }    相机= Camera.open();
    最终的参数P = camera.getParameters();    button.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){            如果(isLighOn){
                Log.i(信息,火炬被关闭!);                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(P);
                camera.stop preVIEW();
                isLighOn = FALSE;
            }其他{                Log.i(信息,火炬被打开!);
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);                camera.setParameters(P);
                camera.start preVIEW();
                isLighOn =真;            }        }
    });}
   }

下面是logcat的错误:

解决方案

It appears that the onPause in your activity is being called, and you're releasing the Camera - this is good. However, you never re-initialize it.

The Android developer guide has this exact example.

Basically, you need to do something like this:

@Override
protected void onPause() {
    super.onPause();

    if (camera != null) {
        camera.release();
        camera = null;
    }
}

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

    if (camera == null) {
        initializeCamera();
    }
}

Then you move all the camera initialization code from your onCreate into a new initializeCamera method.

Note you should not initialize the camera in onCreate, since it is being done in onResume (which is called even on first load).

这篇关于应用程序崩溃时,关闭/重启然后pressing按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:35