-1

I followed a tutorial online about how to create a simple app that can switch on and off your phone's cam-light.

<manifest....>

<uses-permission android:name = "android.permission.CAMERA"/>
    <uses-feature android:name = "android.hardware.camera"/>
</manifest>

This is in android manifest. The problem: it crashes when I press the ON button (made by me, in application)

Stack Trace:

07-17 22:27:13.990: E/AndroidRuntime(1775): FATAL EXCEPTION: main 
07-17 22:27:13.990: E/AndroidRuntime(1775): java.lang.NullPointerException 
07-17 22:27:13.990: E/AndroidRuntime(1775): at com.example.salpa.MainActivity$1.onClick(MainActivity.java:33) 
07-17 22:27:13.990: E/AndroidRuntime(1775): at android.view.View.performClick(View.java:4204) 
07-17 22:27:13.990: E/AndroidRuntime(1775): at android.view.View$PerformClick.run(View.java:17355) 
07-17 22:27:13.990: E/AndroidRuntime(1775): at android.os.Handler.handleCallback(Handler.java:725)

Another strange thing: when I install the app, there are no details about what permissions is requiring.

"The application can acces the following on your phone:" and there's nothing. (despite the permissions in tutorial where it shows that the application needs permision to Camera.)

Can someone please help me, thanks.

setContentView(R.layout.activity_main);

Button aprinde =(Button)findViewById(R.id.aprinde);
//Button sting = (Button)findViewById(R.id.sting);

cameraObj = Camera.open();

aprinde.setOnClickListener(new View.OnClickListener()
{

    public void onClick(View view){

        Camera.Parameters cameraParams = cameraObj.getParameters();

        cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

        cameraObj.setParameters(cameraParams);
        cameraObj.startPreview();


    }

} );
Xan
  • 45
  • 8
  • Please post the stack trace of the crash. If you are installing the app via ADB (Android Studio), then you won't be prompted to approve permissions. – Jon Jul 17 '15 at 23:20
  • ok, what is on line 33 of `MainActivity.java`? – Jon Jul 17 '15 at 23:33
  • http://stackoverflow.com/a/20594332/2350083 possible solution? – Jon Jul 17 '15 at 23:36
  • @Jonathan727 line 26: cameraObj = Camera.open(); – Xan Jul 17 '15 at 23:39
  • please post the entire `onClick` method, or even the whole activity if it isn't too long. What are you doing with the camera object on the five lines in between `Camera.open();` and `cameraObj.getParameters();`? – Jon Jul 17 '15 at 23:41
  • You aren't running this on an emulator, are you? – Jon Jul 17 '15 at 23:52
  • 1
    of course. the app is starting. I made 2 buttons. One is this, that crashes, and another does nothing. I am running the app on emulator and on my phone aswell. Get crash on both. – Xan Jul 17 '15 at 23:53
  • Your issue is likely the emulator (see my answer). For further examples see this great open source flashlight project: https://github.com/sanbeg/flashlight – Jon Jul 18 '15 at 00:05

1 Answers1

0

Your app is crashing because you are running it on an emulator, which doesn't have a back-facing camera.

The documentation for Camera#open():

Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null.

You can fix the crash by checking for null.

public void onClick(View v) {
    if (cameraObj == null) {
        return;
    }
    Camera.Parameters cameraParams = cameraObj.getParameters();
    cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cameraObj.setParameters(cameraParams);
    cameraObj.startPreview();
}

You should also look out for setParameters as this could crash too if the device doesn't have a flash.

public void onClick(View v) {
    if (cameraObj == null) {
        return;
    }
    Camera.Parameters cameraParams = cameraObj.getParameters();
    if (cameraParams.getFlashMode() == null) {
        return;
    }
    cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cameraObj.setParameters(cameraParams);
    cameraObj.startPreview();
}
Jon
  • 9,156
  • 9
  • 56
  • 73