Question

My flashlight app is keep crashing when turn on the light and go to the homescreen, or use the back button or even when I turn on the screen, it doesn't crash right away; when I go back to the app, I press the button to turn off/on the light and it crashes with these log cats;

01-31 09:26:07.094: E/AndroidRuntime(16941): FATAL EXCEPTION: main
01-31 09:26:07.094: E/AndroidRuntime(16941): java.lang.RuntimeException: Method called after release()
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.hardware.Camera.native_setParameters(Native Method)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.hardware.Camera.setParameters(Camera.java:1492)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at de.vogella.android.notificationmanager.NotificationReceiverActivity.onClick(NotificationReceiverActivity.java:67)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.view.View.performClick(View.java:4192)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.view.View$PerformClick.run(View.java:17254)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.os.Handler.handleCallback(Handler.java:615)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.os.Looper.loop(Looper.java:137)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at android.app.ActivityThread.main(ActivityThread.java:4950)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at java.lang.reflect.Method.invokeNative(Native Method)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at java.lang.reflect.Method.invoke(Method.java:511)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-31 09:26:07.094: E/AndroidRuntime(16941):    at dalvik.system.NativeStart.main(Native Method)

And these are my methods;

public void onClick(View v) {

// TODO Auto-generated method stub
if (camOn == false) {
    cam = Camera.open();
    para = cam.getParameters();
    para.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(para);
    Log.i(CLASSNAME, "AA1  "+camOn);
    camOn = true;
    Log.i(CLASSNAME, "AA2  "+camOn);

} 



else {
    para.setFlashMode(Parameters.FLASH_MODE_OFF);
    cam.setParameters(para);
    cam.release();
    //cam = null;
    Log.i(CLASSNAME, "BB1  "+camOn);
    camOn = false;
    Log.i(CLASSNAME, "BB2  "+camOn);

}
}

@Override
protected void onPause() {
super.onPause();
// TODO Auto-generated method stub
   cam.release();
  //  cam=cam;
// finish();*/
}
@Override
protected void onStop() {
super.onStop();
cam.release();

}
@Override
protected void onDestroy() {
super.onDestroy();
cam.release();

}
protected void onResume() {
super.onResume();

}

}
Was it helpful?

Solution 3

I think it was just an issue like any other app randomly crashes on Android device, it was one of those times, because it works now, and I didn't make any changes to the code.

OTHER TIPS

You are calling cam.release() from all of onStop(), onPause() and onDestroy(), so cam.release() will be called three times when your application is destroyed. This is at best unnecessary and, at worst, could cause crashes.

I'd suggest calling Camera.open() only from onResume(), and cam.release() only from onPause(). Remove all the other Camera.open() and release() calls.

You should study the activity lifecycle documentation carefully.

As Martin Stone pointed out, your calls to cam.close/open are sloppy. The real problem is, I believe, that you never call cam.open in onResume.

Closing the app isn't a problem. It's just that it no longer has the required resources when resuming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top