I'm using camera in my application. So in onCreate()
I get camera instance, in onPause()
I release the camera, in onResume()
I get the camera instance if variable is null
. Then I added to my app another activity. From this second activity I wanted to close all activities... close the application. I used this approach:
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
and
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
It worked fine. The application closed. But then when I wanted to start the app again I got force close message. I used step by step debugging and my code was failing on line maCamera = getCameraInstance();
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
maCamera = getCameraInstance();
...
}
The error log:
11-05 00:48:02.289: E/AndroidRuntime(8257): FATAL EXCEPTION: main
11-05 00:48:02.289: E/AndroidRuntime(8257): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla/com.blabla.YoMainActivity}: java.lang.NullPointerException
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.os.Looper.loop(Looper.java:130)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-05 00:48:02.289: E/AndroidRuntime(8257): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 00:48:02.289: E/AndroidRuntime(8257): at java.lang.reflect.Method.invoke(Method.java:507)
11-05 00:48:02.289: E/AndroidRuntime(8257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-05 00:48:02.289: E/AndroidRuntime(8257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-05 00:48:02.289: E/AndroidRuntime(8257): at dalvik.system.NativeStart.main(Native Method)
11-05 00:48:02.289: E/AndroidRuntime(8257): Caused by: java.lang.NullPointerException
11-05 00:48:02.289: E/AndroidRuntime(8257): at com.blabla.YoCameraPreview.<init>(YoCameraPreview.java:26)
11-05 00:48:02.289: E/AndroidRuntime(8257): at com.blabla.YoMainActivity.onCreate(YoMainActivity.java:82)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 00:48:02.289: E/AndroidRuntime(8257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-05 00:48:02.289: E/AndroidRuntime(8257): ... 11 more
I figured that it has something to do with releasing the camera. So I tried to call camera.release()
in onDestroy()
and it worked. No more force close messages.
So my question is: Is this normal behavior? Why isn't calling release in onPause() enough?
p.s. I can post some more code if it is not usual behavior and the problem is probably in my code.
UPDATE:
Methods in my code:
@Override
protected void onDestroy(){
super.onDestroy();
maPreview = null;
releaseCamera();
}
@Override
protected void onResume(){
super.onResume();
if (maCamera == null) {
maCamera = getCameraInstance();
}
if (maPreview == null) {
maPreview = new YoCameraPreview(this, maCamera);
maLayoutPreview.removeAllViews();
maLayoutPreview.addView(maPreview);
}
}
@Override
protected void onPause() {
super.onPause();
maPreview = null;
releaseCamera();
}
private void releaseCamera(){
if (maCamera != null){
maCamera.release();
maCamera = null;
}
}
I don't take pictures with this app. I only use the camera preview, therefore I have no previewCallback set.