我按照这里的教程并设法在我的应用程序中实现闪光灯。然而,我已经陷入困境。
闪光灯按钮在第一次加载应用程序时会产生奇迹,但是当您转到另一个活动(页面)然后返回第一个活动(页面)并尝试使用闪光灯按钮时,它会强制关闭应用程序
这是我加载应用程序的logCat,打开和关闭手电筒导航到另一个活动,然后再次尝试闪光灯并bam dead
08-18 23:12:50.854: I/info(10685): torch is turn on!
08-18 23:12:51.565: D/View(10685): onTouchEvent: viewFlags: 0x18004001
08-18 23:12:51.565: D/View(10685): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false
08-18 23:12:51.565: I/info(10685): torch is turn off!
08-18 23:12:58.522: D/View(10685): onTouchEvent: viewFlags: 0x18004001
08-18 23:12:58.522: D/View(10685): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false
08-18 23:12:58.672: D/ATRecorder(10685): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@4054f238
08-18 23:12:58.682: D/WindowManagerImpl(10685): addView, new view, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@40545f00
08-18 23:12:58.912: D/Camera-JNI(10685): native_release: context=0x34cec0 camera=0x34ce78
08-18 23:13:00.844: W/KeyCharacterMap(10685): Can't open keycharmap file
08-18 23:13:00.854: W/KeyCharacterMap(10685): Error loading keycharmap file '/system/usr/keychars/synaptics-rmi-touchscreen.kcm.bin'. hw.keyboards.65537.devname='synaptics-rmi-touchscreen'
08-18 23:13:00.854: I/KeyCharacterMap(10685): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-18 23:13:01.214: D/WindowManagerImpl(10685): finishRemoveViewLocked, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@40545f00
08-18 23:13:02.205: D/View(10685): onTouchEvent: viewFlags: 0x18004001
08-18 23:13:02.205: D/View(10685): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false
08-18 23:13:02.205: I/info(10685): torch is turn on!
08-18 23:13:02.205: D/AndroidRuntime(10685): Shutting down VM
08-18 23:13:02.205: W/dalvikvm(10685): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
08-18 23:13:02.215: E/AndroidRuntime(10685): FATAL EXCEPTION: main
08-18 23:13:02.215: E/AndroidRuntime(10685): java.lang.RuntimeException: Method called after release()
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.hardware.Camera.native_setParameters(Native Method)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.hardware.Camera.setParameters(Camera.java:958)
08-18 23:13:02.215: E/AndroidRuntime(10685): at com.reading.festival.ReadingFestivalGuide2012Activity$1.onClick(ReadingFestivalGuide2012Activity.java:84)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.view.View.performClick(View.java:2533)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.view.View$PerformClick.run(View.java:9320)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.os.Handler.handleCallback(Handler.java:587)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.os.Looper.loop(Looper.java:150)
08-18 23:13:02.215: E/AndroidRuntime(10685): at android.app.ActivityThread.main(ActivityThread.java:4385)
08-18 23:13:02.215: E/AndroidRuntime(10685): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 23:13:02.215: E/AndroidRuntime(10685): at java.lang.reflect.Method.invoke(Method.java:507)
08-18 23:13:02.215: E/AndroidRuntime(10685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
08-18 23:13:02.215: E/AndroidRuntime(10685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-18 23:13:02.215: E/AndroidRuntime(10685): at dalvik.system.NativeStart.main(Native Method)
用于相机的代码
private boolean isLighOn = false;
private Camera camera;
private Button button;
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//makes full screen and takes away title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
});