手电筒android studio,Android Studio:手电筒关闭时崩溃

我试图开发手电筒应用程序作为我的第一个“真正”的应用程序,几乎所有的工作。但每次我想关闭手电筒时,我的应用程序都会崩溃。代码和logcat的日志如下:Android Studio:手电筒关闭时崩溃

package com.leuchtstein.flashlight;

import android.hardware.Camera;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public boolean stat = false;

TextView text;

Camera camera;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.textView);

}

public void triggerlight(View view) {

if (stat == false) {

camera = Camera.open();

Camera.Parameters parameters = camera.getParameters();

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

camera.setParameters(parameters);

camera.startPreview();

text.setText("ON");

stat = true;

} else {

camera = Camera.open();

Camera.Parameters parameters = camera.getParameters();

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

camera.setParameters(parameters);

camera.stopPreview();

camera.release();

text.setText("OFF");

stat = false;

}

}

}

activity_main.xml中

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.leuchtstein.flashlight.MainActivity">

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="16dp"

android:text="@string/desc_textview"

android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"

app:layout_constraintHorizontal_bias="0.95"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="triggerlight"

android:text="Turn on/off"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toBottomOf="@+id/textView"

app:layout_constraintVertical_bias="0.387" />

logcat的:提前

https://pastebin.com/tNyF6GvX

谢谢,leuchtstein。

编辑:是的,我有用途的许可功能:

package="com.leuchtstein.flashlight">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值