手电筒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">
總結
以上是生活随笔為你收集整理的手电筒android studio,Android Studio:手电筒关闭时崩溃的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux适应环境,Linux从入门到适
- 下一篇: 职中心得体会300字高一计算机,职业高中