Application closing in user interaction after being summarized

0

I'm playing around with an application that controls my mobile's flash , but it does force close as soon as the user interacts with the app after it has been summarized .

For example:

  • I opened the application
  • Turn the flashlight on and off
  • I turned the screen off
  • I turned on the screen and unlocked the phone
  • I tried to turn on 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 did not understand why the error was NullPointerException .

        
    asked by anonymous 22.04.2014 / 20:53

    1 answer

    3

    I think your problem is how you manage "activity lifecycle" .

    Do the following:

    Pass the code that has onStop to 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 in onCreate to 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();
        }
    }
    
        
    22.04.2014 / 21:10