0
I’m playing with an app that controls the flash from my mobile phone, but the same force close as soon as the user interacts with the application, after it has been summarized.
For example:
- I opened the app
- I turned the flashlight on and off
- I turned off the screen
- I turned on the screen and unlocked the phone
- I tried to activate the flashlight
- force close
My code:
package com.bravosix.lanterna;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class Tela_Lanterna extends Activity {
private boolean isFlashOn;
private Camera mCamera;
private ImageButton mImageButton;
private PackageManager mPackage = null;
// private AlertDialog mNoCameraAlert;
private Parameters mParameters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.layout_lanterna);
mPackage = this.getPackageManager();
mImageButton = (ImageButton) findViewById(R.id.toggle_img);
mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggleFlash();
}
});
mCamera = Camera.open();
mParameters = mCamera.getParameters();
if (!verificarFlash(mPackage)) {
Toast.makeText(this, "Dispositivo não possui flash!",
Toast.LENGTH_SHORT).show();
return;
}
}
private void toggleFlash() {
if (!isFlashOn) {
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(mParameters);
mCamera.startPreview();
mImageButton.setImageResource(R.drawable.btn_ligado);
isFlashOn = true;
} else if (isFlashOn) {
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameters);
mCamera.stopPreview();
mImageButton.setImageResource(R.drawable.btn_desligado);
isFlashOn = false;
}
}
@Override
protected void onPause() {
super.onPause();
if (isFlashOn) {
toggleFlash();
}
}
@Override
protected void onStop() {
super.onStop();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
private boolean verificarFlash(PackageManager mPackage) {
if (mPackage.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
return true;
} else {
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_lanterna, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_configs) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My logcat:
04-22 15:46:47.994: E/AndroidRuntime(29553): FATAL EXCEPTION: main
04-22 15:46:47.994: E/AndroidRuntime(29553): Process: com.bravosix.lanterna, PID: 29553
04-22 15:46:47.994: E/AndroidRuntime(29553): java.lang.NullPointerException
04-22 15:46:47.994: E/AndroidRuntime(29553): at com.bravosix.lanterna.Tela_Lanterna.toggleFlash(Tela_Lanterna.java:52)
04-22 15:46:47.994: E/AndroidRuntime(29553): at com.bravosix.lanterna.Tela_Lanterna.access$0(Tela_Lanterna.java:50)
04-22 15:46:47.994: E/AndroidRuntime(29553): at com.bravosix.lanterna.Tela_Lanterna$1.onClick(Tela_Lanterna.java:35)
04-22 15:46:47.994: E/AndroidRuntime(29553): at android.view.View.performClick(View.java:4456)
04-22 15:46:47.994: E/AndroidRuntime(29553): at android.view.View$PerformClick.run(View.java:18462)
04-22 15:46:47.994: E/AndroidRuntime(29553): at android.os.Handler.handleCallback(Handler.java:733)
04-22 15:46:47.994: E/AndroidRuntime(29553): at android.os.Handler.dispatchMessage(Handler.java:95)
04-22 15:46:47.994: E/AndroidRuntime(29553): at android.os.Looper.loop(Looper.java:136)
04-22 15:46:47.994: E/AndroidRuntime(29553): at android.app.ActivityThread.main(ActivityThread.java:5102)
04-22 15:46:47.994: E/AndroidRuntime(29553): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 15:46:47.994: E/AndroidRuntime(29553): at java.lang.reflect.Method.invoke(Method.java:515)
04-22 15:46:47.994: E/AndroidRuntime(29553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 15:46:47.994: E/AndroidRuntime(29553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 15:46:47.994: E/AndroidRuntime(29553): at dalvik.system.NativeStart.main(Native Method)
I don’t understand why the mistake is NullPointerException
.
Thank you, the code worked perfectly. Could you explain to me where my mistake was?
– Renan Lazarotto
I think the mistake lies in the fact that you do the release of the camera in
onStop
and never do againopen
before using it intoggleFlash()
, mCamera is null. Read this about "Activity Lifecycle". Realizing how "Activity Lifecycle" is very important in Android development.– ramaral
Thank you, I’ll read it right away.
– Renan Lazarotto