我有一个flash light app class,如下所示;
public class SimpleNotificationAppActivity extends Activity implements OnClickListener {
Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wl.acquire();
initialize();
ib1.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
ib1 = (ImageButton) findViewById(R.id.ib2);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (cam == null) {
cam = Camera.open();
para = cam.getParameters();
para.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(para);
} else {
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
finish();
}
}
这个类运行得很完美,但如果我进入主屏幕,或关闭屏幕,当应用程序仍在运行时,当我回到应用程序时,它再次设置cam = null,同时cam设置为android .hardware.camera这里是log cat;
01-31 06:36:52.896: I/NotificationReceiverActivity(31188): CREATING NOWnull
01-31 06:36:52.956: I/Adreno200-EGLSUB(31188): <ConfigWindowMatch:2087>: Format RGBA_8888.
01-31 06:36:54.408: I/NotificationReceiverActivity(31188): AAandroid.hardware.Camera@41cc75d0
01-31 06:36:58.912: I/NotificationReceiverActivity(31188): CREATING NOWnull
01-31 06:36:58.952: I/Adreno200-EGLSUB(31188): <ConfigWindowMatch:2087>: Format RGBA_8888.
01-31 06:37:01.365: D/AndroidRuntime(31188): Shutting down VM
01-31 06:37:01.365: W/dalvikvm(31188): threadid=1: thread exiting with uncaught exception (group=0x411d6360)
01-31 06:37:01.365: E/AndroidRuntime(31188): FATAL EXCEPTION: main
01-31 06:37:01.365: E/AndroidRuntime(31188): java.lang.RuntimeException: Fail to connect to camera service
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.hardware.Camera.native_setup(Native Method)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.hardware.Camera.<init>(Camera.java:371)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.hardware.Camera.open(Camera.java:344)
01-31 06:37:01.365: E/AndroidRuntime(31188): at de.vogella.android.notificationmanager.NotificationReceiverActivity.onClick(NotificationReceiverActivity.java:50)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.view.View.performClick(View.java:4192)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.view.View$PerformClick.run(View.java:17254)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.os.Handler.handleCallback(Handler.java:615)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.os.Looper.loop(Looper.java:137)
01-31 06:37:01.365: E/AndroidRuntime(31188): at android.app.ActivityThread.main(ActivityThread.java:4950)
01-31 06:37:01.365: E/AndroidRuntime(31188): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 06:37:01.365: E/AndroidRuntime(31188): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 06:37:01.365: E/AndroidRuntime(31188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-31 06:37:01.365: E/AndroidRuntime(31188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-31 06:37:01.365: E/AndroidRuntime(31188): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
仔细阅读开发者指南http://developer.android.com/reference/android/hardware/Camera.html
中解释的所有10个要点特别是数字10:
要使用此课程拍摄照片,请使用以下步骤:
每次使用完相机后都需要拨打cam.release();
。
答案 1 :(得分:0)
检查onClick
中的逻辑...每次点击它时你都设置cam = null;
并且不是空的。