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

时间:2013-07-03 01:34:22

标签: android

我正在运行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

1 个答案:

答案 0 :(得分:3)

您的活动中的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中完成的(即使在首次加载时也会调​​用)。