尝试打开手电筒时应用崩溃

时间:2013-01-10 09:44:12

标签: android flashlight

我试图制作Flahlight应用,但当我按下"手电筒"按钮打开手电筒,应用程序崩溃。 这是我的代码:

清单:

<?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();
    }

}

我把代码放在图片中,因为我无法让代码块工作。

3 个答案:

答案 0 :(得分:1)

将参数View v添加到public void toggleFlashlight()

作为

public void toggleFlashlight(View v)

答案 1 :(得分:0)

变化:

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

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();
}

从您的logcat中可以看出,您正在将XML中的onClick方法设置为toggleFlashlight()。由于是一个在View上单击调用的方法,它必须与其他onClick()方法的签名匹配,并且具有View参数。

答案 2 :(得分: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;
    }
}