关于应用程序:
我正在尝试制作一个简单的手电筒应用程序,其 UI 中只有一个切换按钮。由于我希望即使在应用程序最小化时手电筒也能打开,所以我在 onClick()事件中打开 并释放了相机。
问题:
我的应用程序在第一次打开时工作。尝试多次打开和关闭手电筒,它可以工作。但是,当我重新启动应用程序时,它会意外关闭。
一个很大的问题是当我第一次启动应用程序时,在没有使用应用程序的情况下按下返回按钮并重新启动应用程序,它仍然会意外关闭。如果我第一次启动我的应用程序,请在不使用该应用程序的情况下按返回并启动我的相机,出现“相机不可用”吐司。(我的代码只声明了两个变量,并且在 onCreate() 部分中绝对没有添加任何代码,所以我不知道我的应用程序如何/为什么持有相机资源)。
另一个问题是模拟器显示没有错误。(╯°□°)╯︵┻━┻</p>
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)
{}
}
}