gpt4 book ai didi

android - 关闭/重新打开然后按下按钮时应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:04 25 4
gpt4 key购买 nike

我正在运行 eclipse 并诚实地自学 android 编程的基础知识。我想我会从一个简单的手电筒应用程序开始,这样我就可以更好地理解它是如何工作的。该应用程序本身运行良好并启动相机闪光灯。

我的问题是,当我按下主页按钮,然后返回到应用程序并按下按钮打开手电筒时,应用程序崩溃了。我读了很多书,我猜这是因为我没有“onResume”,但我可能错了。非常感谢任何帮助。

  import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.pmm.lettherebe.R;

public class MainActivity extends Activity {

//flag to detect flash is on or off
private boolean isLighOn = false;

private Camera camera;

private Button button;

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

if (camera != null) {
camera.release();;
}
}




@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonFlashlight);

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

// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}

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

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

if (isLighOn) {
Log.i("info", "torch is turn off!");

p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;


} else {

Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);

camera.setParameters(p);
camera.startPreview();
isLighOn = true;

}

}
});

}
}

这是 logcat 错误:

 07-02 20:32:19.680: D/OpenGLRenderer(14897): Enabling debug mode 0
07-02 20:32:21.121: I/info(14897): torch is turn on!
07-02 20:32:22.553: I/info(14897): torch is turn off!
07-02 20:32:23.363: I/info(14897): torch is turn on!
07-02 20:32:23.964: I/info(14897): torch is turn off!
07-02 20:32:29.090: I/info(14897): torch is turn on!
07-02 20:32:29.090: D/AndroidRuntime(14897): Shutting down VM
07-02 20:32:29.090: W/dalvikvm(14897): threadid=1: thread exiting
withuncaught exception (group=0x41b46930)
07-02 20:32:29.090: E/AndroidRuntime(14897): FATAL EXCEPTION: main
07-02 20:32:29.090: E/AndroidRuntime(14897): java.lang.RuntimeException: Method
alled after release()
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.hardware.Camera.native_setParameters(Native Method)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.hardware.Camera.setParameters(Camera.java:1496)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
com.pmm.lettherebe.MainActivity$1.onClick(MainActivity.java:74)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.view.View.performClick(View.java:4204)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.view.View$PerformClick.run(View.java:17355)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.os.Handler.handleCallback(Handler.java:725)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.os.Handler.dispatchMessage(Handler.java:92)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.os.Looper.loop(Looper.java:137)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
android.app.ActivityThread.main(ActivityThread.java:5195)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
java.lang.reflect.Method.invokeNative(Native Method)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
java.lang.reflect.Method.invoke(Method.java:511)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-02 20:32:29.090: E/AndroidRuntime(14897): at
dalvik.system.NativeStart.main(Native Method)
07-02 20:32:31.222: I/Process(14897): Sending signal. PID: 14897 SIG: 9

最佳答案

您的 Activity 中的 onPause 似乎正在被调用,并且您正在释放相机 - 这很好。但是,您永远不会重新初始化它。

Android 开发者指南有 this exact example .

基本上,您需要执行以下操作:

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

if (camera != null) {
camera.release();
camera = null;
}
}

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

if (camera == null) {
initializeCamera();
}
}

然后将所有相机初始化代码从 onCreate 移动到新的 initializeCamera 方法中。

请注意,您不应在 onCreate 中初始化相机,因为它是在 onResume 中完成的(即使在首次加载时也会调​​用)。

关于android - 关闭/重新打开然后按下按钮时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17438395/

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