我的android活动部队在不规则的时间关闭。它要做的只是打开LED手电筒。这是logcat中的错误:

D/CameraService(   70): CameraService::connect E (pid 4598, id 0)
I/CameraService(   70): cameraId:0
I/CameraService(   70): 2D mode only
W/CameraService(   70): CameraService::connect X (pid 4598) rejected (existing client).
D/AndroidRuntime( 4598): Shutting down VM
W/dalvikvm( 4598): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
E/AndroidRuntime( 4598): FATAL EXCEPTION: main
E/AndroidRuntime( 4598): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cakemansapps.lightwriter/com.cakemansapps.lightwriter.LightWriter}: java.lang.RuntimeException: Fail to connect to camera service
E/AndroidRuntime( 4598):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
E/AndroidRuntime( 4598):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
E/AndroidRuntime( 4598):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3288)
E/AndroidRuntime( 4598):    at android.app.ActivityThread.access$1600(ActivityThread.java:132)
E/AndroidRuntime( 4598):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1042)
E/AndroidRuntime( 4598):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4598):    at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime( 4598):    at android.app.ActivityThread.main(ActivityThread.java:4263)
E/AndroidRuntime( 4598):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4598):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 4598):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 4598):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 4598):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4598): Caused by: java.lang.RuntimeException: Fail to connect to camera service
E/AndroidRuntime( 4598):    at android.hardware.Camera.native_setup(Native Method)
E/AndroidRuntime( 4598):    at android.hardware.Camera.<init>(Camera.java:265)
E/AndroidRuntime( 4598):    at android.hardware.Camera.open(Camera.java:241)
E/AndroidRuntime( 4598):    at com.cakemansapps.lightwriter.LightWriter.onCreate(LightWriter.java:21)
E/AndroidRuntime( 4598):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
E/AndroidRuntime( 4598):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
E/AndroidRuntime( 4598):    ... 12 more
W/ActivityManager(  123):   Force finishing activity com.cakemansapps.lightwriter/.LightWriter
V/AgentService_J(30842): Action[1520]:com.htc.android.iqagent.action.ui03
I/HTC_SUBMITTER_C(30842): (-3) submitUI03:0,22,0,java.lang.RuntimeException: Fail to connect to camera service
V/AgentService_J(30842): (-3)ErrorCode:0,AppType:22,Level:0,Message:java.lang.RuntimeException: Fail to connect to camera service


这是我的活动。没有其他课程。

package com.cakemansapps.lightwriter;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.view.KeyEvent;

public class LightWriter extends Activity implements OnTouchListener {
   private static final String TAG = "Touch" ;
   private Camera mCamera;
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
      mCamera = Camera.open();
   }

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                Log.w("LightWriter", "Down");
                setFlashlight(true);
            }
            else if (action == KeyEvent.ACTION_UP) {
                Log.w("LightWriter", "Up");
                setFlashlight(false);
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }
/***
 * Attempts to set camera flash torch/flashlight mode on/off
 * @param isOn true = on, false = off
 * @return boolean whether or not we were able to set it
 */

public boolean setFlashlight(boolean isOn)
{
    if (mCamera == null)
    {
        return false;
    }
    Camera.Parameters params = mCamera.getParameters();
    String value;
    if (isOn) // we are being ask to turn it on
    {
        value = Camera.Parameters.FLASH_MODE_TORCH;
    }
    else  // we are being asked to turn it off
    {
        value =  Camera.Parameters.FLASH_MODE_OFF;
    }

    try{
        params.setFlashMode(value);
        mCamera.setParameters(params);

        String nowMode = mCamera.getParameters().getFlashMode();

        if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
        {
            return true;
        }
        if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
        {
            return true;
        }
        return false;
    }
    catch (Exception ex)
    {
    }
    return false;
}
    public boolean onTouch(View view, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
@Override
public void onBackPressed() {
    mCamera.release();
    this.finish();
    return;
   }
}


我做错什么了?

最佳答案

请勿将相机服务连接保存在字段中。取而代之的是,在您将要使用该服务时调用open,并在完成后释放。

08-26 05:06