gpt4 book ai didi

ANDROID 应用程序在第二次打开时关闭,第一次打开时工作正常

转载 作者:行者123 更新时间:2023-11-29 21:31:27 25 4
gpt4 key购买 nike

关于应用程序:

我正在尝试制作一个简单的手电筒应用,其用户界面中只有一个切换按钮。因为我希望手电筒即使在应用程序最小化时也能打开,所以我打开 并在onClick() 事件中释放 相机。

问题:

  • 我的应用程序在首次打开时运行正常。尝试多次打开和关闭手电筒,它可以正常工作。但是,当我重新启动应用程序时,它意外关闭。

  • 一个大问题是当我第一次启动该应用程序时,在没有使用该应用程序的情况下按下后退按钮并重新启动该应用程序,它仍然意外关闭。如果我第一次启动我的应用程序,在不使用该应用程序的情况下按回车键并启动我的相机,则会出现“相机不可用” toast 。(我的代码只声明了两个变量,并且在 onCreate 中绝对没有添加任何代码() 部分,所以我不知道我的应用如何/为什么持有相机资源)。

  • 另一个问题是模拟器没有显示错误。 (╯°□°)╯︵┻━┻

PS:android.permission.CAMERA 已设置权限

代码:

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

Camera camera=null;
Parameters p;

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

public void flash(View view) {
// Is the toggle button on?
boolean on = ((ToggleButton) view).isChecked();

if (on) {
// Enable torch
camera = Camera.open();
p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
} else {
// Disable torch
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release();
}
}
}

应用程序在启动应用程序时不会在模拟器上崩溃,在不使用和重新启动应用程序的情况下按下返回(与我的手机不同)。但仍在发布 LogCat。

第一次启动应用程序时:

10-15 16:05:50.229: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.529: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.529: I/Choreographer(11244): Skipped 49 frames! The application may be doing too much work on its main thread.
10-15 16:05:51.539: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.749: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.799: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.999: D/dalvikvm(11244): GC_CONCURRENT freed 128K, 9% free 2643K/2888K, paused 30ms+98ms, total 700ms
10-15 16:05:51.999: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.019: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.031: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.139: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.139: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.189: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.219: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.249: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.249: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.320: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.400: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0

关于回击:

10-15 16:08:05.559: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.579: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.609: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.629: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.720: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.908: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.919: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.929: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:06.169: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:06.189: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:07.149: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:07.359: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.009: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.009: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.029: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.219: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.219: I/Choreographer(11244): Skipped 118 frames! The application may be doing too much work on its main thread.

重启时:

10-15 16:08:47.629: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:48.909: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:48.909: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.108: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.169: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.379: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.389: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.389: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.450: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.489: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.500: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.500: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.509: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.519: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.619: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.730: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0

附加信息:我检查了发布在该网站上的这两个问题,发现一个不相关,另一个太复杂。 ( url1 url2 )

** 修改了代码使其工作,但我仍然不知道以前问题的原因。 **

新代码:

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

Camera camera=null;
Parameters p;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = Camera.open();
p = camera.getParameters();
}

public void flash(View view) {
// Is the toggle button on?
boolean on = ((ToggleButton) view).isChecked();

if (on) {
// Enable torch
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
} else {
// Disable torch

p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
}
}

protected void onResume()
{
super.onResume();
try
{
camera.reconnect();
}
catch(Exception e)
{}
}

protected void onPause()
{
super.onPause();
try
{
camera.release();
}
catch(Exception e)
{}
}

protected void onStop()
{
super.onStop();
try
{
camera.release();
}
catch(Exception e)
{}
}
}

最佳答案

我相信您的大部分问题是由于在离开应用程序时没有正确关闭 Camera 资源造成的。如果你在灯亮着的时候这样做,那么你还没有释放 resources

According to the docs使用 Camera object 的最后一步是这样说的:

Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

因此,您可能应该重写 onPause() 并添加它以确保下次运行应用程序或尝试使用相机时 Camera 资源可用一个不同的应用程序。

@Override
public void onPause()
{
camera.release();
super.onPause();
}

另外,您应该在您的应用崩溃时发布您的 logcat,以便我们可以准确地看到异常的内容和位置。

关于ANDROID 应用程序在第二次打开时关闭,第一次打开时工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384886/

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