Question

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.

Was it helpful?

Solution

Add parameter View v to public void toggleFlashlight()

as

public void toggleFlashlight(View v)

OTHER TIPS

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.

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top