0

I'm trying to make a Flahlight app, but when I press the "Flashlight" button to turn the flashlight on, the app crashes. Here's my code:

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.flashlight"
   android:versionCode="1"
   android:versionName="1.0" >

    <!-- Allows access to the flashlight -->
        <permission android:name="android.permission.FLASHLIGHT"
            android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
            android:protectionLevel="normal" />

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

    <uses-sdk
       android:minSdkVersion="5"
       android:targetSdkVersion="16" />

    <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
        <activity
           android:name="com.example.flashlight.MainActivity"
           android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

java:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void toggleFlashlight() {
        Camera cam;
        cam = Camera.open();     
        Parameters p = cam.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(p);
        cam.startPreview();
    }

}

I put the codes in images because I couldn't get the code block to work.

thepoosh
  • 12,497
  • 15
  • 73
  • 132
Qruez
  • 3
  • 5

3 Answers3

1

Add parameter View v to public void toggleFlashlight()

as

public void toggleFlashlight(View v)
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

Change:

public void toggleFlashlight() {
    Camera cam;
    cam = Camera.open();     
    Parameters p = cam.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();
}

to

public void toggleFlashlight(View v) {
    Camera cam;
    cam = Camera.open();     
    Parameters p = cam.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();
}

From your logcat, it is seen that you're setting the method for onClick in your XML to toggleFlashlight(). Due to being a method called on a View's click, it must match the signature of other onClick() methods, and have a View parameter.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0
private void switchOn()
{
       if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
    {
        if(cam == null)
        {
            cam = Camera.open();
            Parameters p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(p);
            cam.startPreview();
        }
    }
}

private void switchOff()
{
    if(cam != null)
    {
        cam.stopPreview();
        cam.release();
        cam = null;
    }
}
lkdg
  • 1,031
  • 7
  • 18
  • Thank you so much it works :) Wow I really appreciate your effort (all of you guys) Thanks again! – Qruez Jan 10 '13 at 10:34
  • Now you should overwrite onStop, onStart and onDestroy. Because if you switch your flashlight to on and click the "homebutton" of your device and open the camera of the device, this will crash and vice versa. – lkdg Jan 10 '13 at 10:39