Android Flashlight force closing on return from other activity
-
27-06-2021 - |
Question
I followed the tutorial here and managed to implement the flash light into my app. However I have come unstuck.
The flash light button works wonders upon first load of the app, but when you go to another activity (page) and then come back to the first activity (page) and try to use the flash light button it force closes the app
Here is the logCat I loaded the app, turned torch on and off navigated to another activity and back then tried the flash light again and 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)
code used for camera
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;
}
}
});
Solution
You have called camera.setparameters()
after calling camera.release()
.
You should not call setparameters after releasing it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow