当我的相机改变方向时,我的应用程序无缘无故崩溃

时间:2014-02-10 13:01:04

标签: java android crash camera orientation

好的,也许是有原因的。我还是新手。这是我的代码。在完成方法内部。我在这一行收到错误:camera.setParameters(p);当我转动相机或手机时。这就是导致错误的行。如果我评论出来。相机可能会留在上面。然后我在camera.stopPreview();行上收到错误。

public void strobeTimer182() {
superStrobe = new CountDownTimer(857, 10) {

    public void onTick(long millisUntilFinished) {
        textView2.setText("seconds remaining: " + millisUntilFinished
                / 2);

        if (millisUntilFinished % 5 == 0
                || millisUntilFinished % 2 == 0
                || millisUntilFinished % 3 == 0
                || millisUntilFinished % 7 == 0) {

            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(p);
            camera.startPreview();

        } else {

            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(p);
            camera.stopPreview();
            textView2.setText("off");
        }

    }

    public void onFinish() {
        textView2.setText("done!");
        // Set the flashmode to off
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        // Pass the parameter ti camera object
        camera.setParameters(p);
        //camera.stopPreview();
        textView2.setText("off");

    }
}.start();
superStrobe.onFinish();

}

2 个答案:

答案 0 :(得分:0)

将此添加到您的活动中。希望它有助于:)

android:configChanges="orientation|keyboardHidden|screenSize"

答案 1 :(得分:0)

        public void strobeTimer182() {
    superStrobe = new CountDownTimer(857, 1) {

        public void onTick(long millisUntilFinished) {

            if (millisUntilFinished % 2 == 0) {

                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();

            } else {

                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();

            }
            if (millisUntilFinished == 0) {

                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();

            }

        }

        public void onFinish() {
            // causes errors
        }
    }.start();
    superStrobe.onFinish();
}

        public void strobeTimer182() {
    superStrobe = new CountDownTimer(857, 1) {

        public void onTick(long millisUntilFinished) {



                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();


        }

        public void onFinish() {
            // causes errors
        }
    }.start();
    superStrobe.onFinish();
}

第一个选项是,如果要循环通过三个以上的状态,然后将其设置回第一个状态。

第二个用于频闪的开关效果,但我选择了第一个。