Application closing in user interaction after being summarized

Asked

Viewed 262 times

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:

  1. I opened the app
  2. I turned the flashlight on and off
  3. I turned off the screen
  4. I turned on the screen and unlocked the phone
  5. I tried to activate the flashlight
  6. 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.

1 answer

3


I think your problem lies in the way you manage "Activity Lifecycle".

Do the following:

Pass the code you have on onStop for onPause

@Override
protected void onPause() {
    super.onPause();
    if (isFlashOn) {
        toggleFlash();
    }
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }  

Pass the part of the code referring to the camera that is on onCreatefor onResume

@Override
protected void onResume() {
    super.onResume();
    if (mCamera == null) {
        mCamera = Camera.open();
    }
    if(mPackage == null){
        mPackage = this.getPackageManager();
    }
    mParameters = mCamera.getParameters();
    if (!verificarFlash(mPackage)) {
        Toast.makeText(this, "Dispositivo não possui flash!",
                Toast.LENGTH_SHORT).show();
        finish();
    }
}
  • Thank you, the code worked perfectly. Could you explain to me where my mistake was?

  • I think the mistake lies in the fact that you do the release of the camera in onStop and never do again open before using it in toggleFlash(), mCamera is null. Read this about "Activity Lifecycle". Realizing how "Activity Lifecycle" is very important in Android development.

  • Thank you, I’ll read it right away.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.