单击图标后几秒内手电筒应用程序崩溃

Flashlight app crash within secs of icon clicked

I have created simple flashlight app. There isn't any error at compile. but when the icon is clicked it just stop unexpectedly. I have tried in both physical android phone and emulator. both comes up with same result. I cant see any problem in the code. can someone recify this problem.

Flashlight.java (Activity)

package flashlight.turnmeyon.crystal.laser;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class Flashlight extends AppCompatActivity {

ImageButton imagebutton;
Camera camera;
Camera.Parameters parameters;
boolean isflash = false;
boolean ison = false;

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

    imagebutton = (ImageButton) findViewById(R.id.buttonOn);

    if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        camera.open();
        parameters = camera.getParameters();
        isflash = true;
    }
    imagebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isflash) {
                if (!ison) {
                    imagebutton.setImageResource(R.drawable.buttonon);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    ison = true;
                } else {
                    imagebutton.setImageResource(R.drawable.buttonof);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    camera.stopPreview();
                    ison = false;
                }
                }
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(Flashlight.this);
                builder.setTitle("Error...........");
                builder.setMessage("Flashlight is not avaliable");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
                AlertDialog alertdialog = builder.create();
                alertdialog.show();
            }
        }
    });
}
@Override
protected void onStop() {
    super.onStop();
    if (camera != null) {
        camera.release();
        camera = null;
    }
}
}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#757575"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageButton
android:id="@+id/buttonOn"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#757575"
android:src="@drawable/buttonof"
android:scaleType="fitCenter"
android:layout_marginTop="80dp"
android:layout_marginRight="80dp"
android:layout_marginLeft="80dp"
android:layout_marginBottom="40dp"
android:layout_centerVertical="true"
/>

</RelativeLayout>

这里供您参考。

我创建了 Flash 示例。请查看以下代码。

public class MainActivity extends AppCompatActivity {

    ImageView switch_on_off;
    private Camera camera;
    Parameters params;
    public boolean isSupportFlash, isFlashLighOn;
    String status;

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

        isSupportFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (!isSupportFlash) {
            Toast.makeText(getApplicationContext(), "Your device does not support flash. ", Toast.LENGTH_LONG).show();
        }
        switch_on_off = (ImageView) findViewById(R.id.switch_on_off);

        connectCameraService();
        switch_on_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFlashLighOn) {
                    offFlashLight();
                    switch_on_off.setImageResource(R.drawable.switch_off);
                } else {
                    onFlashLight();
                    switch_on_off.setImageResource(R.drawable.switch_on);
                }
            }
        });
    }

    public void onFlashLight() {
        if (!isFlashLighOn) {
            status = "ON";
            if (camera == null || params == null) {
                return;
            }
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashLighOn = true;
            switch_on_off.setImageResource(R.drawable.switch_on);

        }
    }

    public void offFlashLight() {
        if (isFlashLighOn) {
            status = "ON";
            if (camera == null || params == null) {
                return;
            }
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashLighOn = false;
            switch_on_off.setImageResource(R.drawable.switch_off);
        }
    }

    public void connectCameraService() {
        if (camera == null) {
            camera = android.hardware.Camera.open();
            params = camera.getParameters();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        offFlashLight();
    }

    @Override
    protected void onPause() {
        super.onPause();
        offFlashLight();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        /*if (isSupportFlash) {
            onFlashLight();
        }*/
    }

    @Override
    protected void onStart() {
        super.onStart();
        connectCameraService();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (camera != null) {
            camera.release();
            camera = null;

        }
    }
}

需要权限

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