我试图开发手电筒应用程序作为我的第一个“真正”的应用程序,几乎所有的工作。但每次我想关闭手电筒时,我的应用程序都会崩溃。代码和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">

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

  1. 手电筒android studio,Android QuickSetting---手电筒控制

    今天遇到问题 打开手电筒,再进入相机下拉通知栏中有手电筒已开启的提示.现总结与手电筒相关的知识点. 在下拉通知栏打开手电筒的代码位于"frameworks/base/packages/Sys ...

  2. android studio turn off hyperv,Android Studio 3.1.2 - 关闭VCS(Android Studio 3.1.2 - Turn VCS OFF)

    Android Studio 3.1.2 - 关闭VCS(Android Studio 3.1.2 - Turn VCS OFF) 我最近做了一个更新我的Android Studio到版本3.1.2. ...

  3. android 运行apk时崩溃,从Android Studio运行时安装APK时应用程序崩溃完美无缺

    我最近将 Android Studio更新为2.3,将SDK更新为25.现在,当我在我的手机或模拟器上运行Android Studio应用程序时,它运行正常但是当我生成APK或从应用程序获取APK并在 ...

  4. android app启动图片 加动画效果,Android Studio开发APP启动程序时开屏简单动画效果快速有效解决方案...

    Android Studio开发APP启动程序时开屏简单动画效果快速有效解决方案 大家在设计APP的末期,都会想给APP搞一些"花里胡哨"的特效来提高APP的B格.博主表示亲测有效 ...

  5. 错误 程序包android.app不存在,运行.app时Android Studio获取包名称错误

    运行.app时Android Studio获取包名称错误 我将程序包名称从com.mycompany.myapplication更改为com.mycompany.testapp. 一切都构建良好,但是 ...

  6. Android当方法总数超过64K时(Android Studio)

    1. 问题描述 Error:The number of method references in a .dex file cannot exceed 64K. Error:Execution fail ...

  7. [安卓开发] Android 自己做双向手电筒 源代码

    水平这么烂真是惭愧.. 这年头,想找一个没有广告,体积小巧的手电筒都不容易(好吧我知道大部分手机已经自己带了) 除了闪光灯以外,屏幕也可以用作手电筒(适合于那些没有闪光灯的平板或山寨机..),即把背景 ...

  8. vs android设备仿真器,适用于Android的Visual Studio模拟器 – 运行模拟设备需要内部虚拟网络交换机...

    问题 我想使用Visual Studio Emulator for Android,但每当我尝试启动它时都会失败. 日志的最后部分是: 27.9.2015. 11:10:38: [Informatio ...

  9. Android (2) —— Android Studio找不到连接的手机

    前言 当前,深度学习有很多框架:tensorflow.pytorch.caffe.keras等.很多场景下,需要训练好的模型在移动端运行.移动端的框架又有很多TensorFlow Lite.Core ...

最新文章

  1. 采访Jesús Fernandes(QA主管)
  2. 李开复现身说法成功的十个启发
  3. WPF学习笔记-第二周【基本笔刷】
  4. QT5_PYQT导入自己的图片
  5. Visio中如何画正弦波
  6. python调用函数怎么错_python调用函数失败是什么原因
  7. 【名额有限】腾讯技术工程-运维技术沙龙
  8. binlog日志整理
  9. MDK 中 [WEAK] 的作用
  10. html缓存失败是什么,HTML5离线“应用程序缓存错误事件:清单读取失败(-1)”...
  11. 分页,在第一页不显示上一页或禁止使用上一页
  12. 使用VSTO开发Excel WorkBook文档级项目中出现的Bug及处理办法
  13. windows11 安全中心点击进去内容空白解决方法
  14. Linux下邮箱客户端推荐
  15. 如何在服务器上快速克隆虚拟机,制作虚拟机模板快速克隆Linux虚拟机
  16. 蓝鲸作业平台:搞定与脚本相关的一切
  17. 如何在计算机查找类型文件类型,怎样在电脑中查找某一类型文件?比如查 txt...
  18. iOS开发中extension的用法(延展)
  19. 重磅:世纪大和解!阿里、腾讯生态将打通
  20. 华为服务器 阵列信息查看,服务器查看磁盘阵列

热门文章

  1. 如何用android studio测试用例,Android Studio上使用Appium创建测试用例基本步骤
  2. layui 如何去dom_layui 的基本使用介绍
  3. 设置为true有什么区别_海绵与珍珠棉有什么区别?天南包装小编为您分析
  4. 冯山C语言3 15,冯山
  5. 利用键盘钩子捕获Windows键盘动作
  6. 解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid
  7. Mybatis使用generator自动生成映射配置文件信息
  8. 利用Struts拦截器限制上传图片的格式和大小
  9. 知识在于积累和分类,厚积薄发
  10. Java 的 IO 流