gpt4 book ai didi

java - 恢复应用程序时应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:08 25 4
gpt4 key购买 nike

我正在创建一个简单的手电筒应用程序,但每次我离开应用程序并重新打开它时,它都会崩溃。我是否在我的代码中遗漏了什么,因为我不确定是什么,下面是它崩溃时的错误。请让我知道我需要做什么来解决这个简历问题:

package com.example.gkvxm.materiallight;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;

import com.dd.CircularProgressButton;

import java.io.IOException;


public class FlashLightActivity extends Activity implements SurfaceHolder.Callback {

private boolean isLigtOn = false;

private Camera camera;

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

SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}

@Override
protected void onStop(){
super.onStop();

if(camera!=null){
camera.release();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);

Context context = this;
PackageManager pm = context.getPackageManager();

if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(FlashLightActivity.this, "Your Device is not supported", Toast.LENGTH_SHORT).show();
Log.e("err", "Device is not supported");
return;
}

camera = Camera.open();
final Camera.Parameters p = camera.getParameters();


final CircularProgressButton circularButton1 = (CircularProgressButton) findViewById(R.id.btnWithText);
circularButton1.setIndeterminateProgressMode(true);
circularButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isLigtOn) {
turnOffFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights Off!", Toast.LENGTH_SHORT).show();

} else {
turnOnFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights On!", Toast.LENGTH_SHORT).show();

}

if (circularButton1.getProgress() == 0) {
simulateSuccessProgress(circularButton1);
} else {
circularButton1.setProgress(0);
}
}
});
}

private void turnOnFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

camera.setParameters(p);
camera.startPreview();
isLigtOn = true;
}
private void turnOffFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLigtOn = false;
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){

}
@Override
public void surfaceCreated(SurfaceHolder holder){
try{
Log.i("SurfaceHolder","Setting preview");
camera.setPreviewDisplay(holder);
} catch (IOException e){
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("SurfaceHOlder", "stopping preview");
camera.stopPreview();
holder = null;
}

private void simulateSuccessProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
}
});
widthAnimation.start();
}

private void simulateErrorProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 99);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
if (value == 99) {
button.setProgress(-1);
}
}
});
widthAnimation.start();
}


}

错误:

05-22 03:08:35.646  13909-13909/com.example.gkvxm.materiallight E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.gkvxm.materiallight, PID: 13909
java.lang.RuntimeException: Camera is being used after Camera.release() was called
at android.hardware.Camera._stopPreview(Native Method)
at android.hardware.Camera.stopPreview(Camera.java:732)
at com.example.gkvxm.materiallight.FlashLightActivity.surfaceDestroyed(FlashLightActivity.java:129)
at android.view.SurfaceView.updateWindow(SurfaceView.java:564)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:238)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8785)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1318)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

调用 Camera.release() 后正在使用相机。因此,查看 Activity 生命周期,您需要在 onStart()onRestart() 中打开相机。所以也许这段代码可以正常工作......

package com.example.gkvxm.materiallight;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;

import com.dd.CircularProgressButton;

import java.io.IOException;


public class FlashLightActivity extends Activity implements SurfaceHolder.Callback {

private boolean isLigtOn = false;

private Camera camera;

@Override
protected void onStart(){
super.onStart();
camera = Camera.open();
final Camera.Parameters p = camera.getParameters();
SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}

@Override
protected void onStop(){
super.onStop();

if(camera!=null){
camera.release();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);

Context context = this;
PackageManager pm = context.getPackageManager();

if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(FlashLightActivity.this, "Your Device is not supported", Toast.LENGTH_SHORT).show();
Log.e("err", "Device is not supported");
return;
}

camera = Camera.open();
final Camera.Parameters p = camera.getParameters();


final CircularProgressButton circularButton1 = (CircularProgressButton) findViewById(R.id.btnWithText);
circularButton1.setIndeterminateProgressMode(true);
circularButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isLigtOn) {
turnOffFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights Off!", Toast.LENGTH_SHORT).show();

} else {
turnOnFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights On!", Toast.LENGTH_SHORT).show();

}

if (circularButton1.getProgress() == 0) {
simulateSuccessProgress(circularButton1);
} else {
circularButton1.setProgress(0);
}
}
});
}

private void turnOnFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

camera.setParameters(p);
camera.startPreview();
isLigtOn = true;
}
private void turnOffFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLigtOn = false;
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){

}
@Override
public void surfaceCreated(SurfaceHolder holder){
try{
Log.i("SurfaceHolder","Setting preview");
camera.setPreviewDisplay(holder);
} catch (IOException e){
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("SurfaceHOlder", "stopping preview");
camera.stopPreview();
holder = null;
}

private void simulateSuccessProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
}
});
widthAnimation.start();
}

private void simulateErrorProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 99);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
if (value == 99) {
button.setProgress(-1);
}
}
});
widthAnimation.start();
}
}

关于java - 恢复应用程序时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30390450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com