ANDROID App第二次打开时关闭,第一次打开时工作正常

时间:2013-10-15 15:15:16

标签: android

关于应用

我正在尝试制作一个简单的手电筒应用程序,其UI中只有一个togglebutton。 由于即使应用程序最小化我想要手电筒打开,我也打开  并且在onClick()事件中发布了相机

问题:

  • 首次打开时我的应用有效。尝试多次打开和关闭手电筒并且工作正常。但是,当我重新启动应用时,它会意外关闭。

  • 一个很大的问题是当我第一次启动应用程序时,按下后退按钮而不使用应用程序并重新启动应用程序,它仍然意外关闭。 如果我是第一次启动我的应用程序,请在不使用应用程序的情况下按回并启动我的相机,“Camera not available”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)
    {}
}
}

1 个答案:

答案 0 :(得分:2)

我相信您的大部分问题都是由于在离开应用时没有正确关闭Camera资源。如果您在灯亮起时执行此操作,则表示您尚未释放resources

According to the docs使用Camera object的最后一步说明了这一点:

  

重要事项:调用release()以释放相机以供其他应用程序使用。应用程序应立即在onPause()中释放相机(并在onResume()中重新打开())。

因此,您应该覆盖onPause()并添加此内容,以确保下次运行应用或尝试在其他应用中使用相机时Camera资源可用。

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

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