想让一个使用照相机的应用弹出一个有多种选项对话框,结果一直出错

361055016 发布于 2013/04/09 17:14
阅读 519
收藏 0

想让一个使用照相机的应用弹出一个有多种选项对话框,结果一直出错

程序如下(这是其中的一个java文件):

package com.Camera;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.Camera.R;
import com.cameraView.CameraView;

public class CameraActivity extends Activity implements OnClickListener {
 /** Called when the activity is first created. */
 ImageView imageView;
 boolean isClicked = false;
 CameraView mCameraView;
 Button btn1, btn2, btn3;

 public static Activity activity = null;

 private Handler mHandler = new Handler() {
  public void handleMessage(android.os.Message msg) {
   if (msg.what == 1033) {
    Bitmap bitmap = (Bitmap)msg.obj;
    imageView.setImageBitmap(bitmap);
   }
  };
 };

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
  setContentView(R.layout.main);
  
  activity = this;
  findView();
 }

 private void findView() {
  btn1 = (Button) findViewById(R.id.myButton1);
  btn2 = (Button) findViewById(R.id.myButton2);
  btn3 = (Button) findViewById(R.id.myButton3);
  imageView = (ImageView)findViewById(R.id.myImageView1);
  mCameraView = (CameraView) findViewById(R.id.mSurfaceView1);
  mCameraView.setHandle(mHandler);
  btn1.setOnClickListener(this);
  btn2.setOnClickListener(this);
  btn3.setOnClickListener(this);
 }

 /**
  * 当按返回键时的提示对话框,采用默认样式
  */
 protected void dialog() {
  AlertDialog.Builder builder = new Builder(this);
  builder.setMessage("确认退出吗?");

  builder.setTitle("提示");

  builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();

    CameraActivity.this.finish();
   }
  });

  builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
   }
  });

  builder.create().show();
 }

 
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
   dialog();
  }
  return false;
 }

  
 
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch (v.getId()) {
  case R.id.myButton1:
   mCameraView.startCamera();
   break;
  case R.id.myButton2:
   mCameraView.stopCamera();
   break;
  case R.id.myButton3:
   mCameraView.tackPicture();
   break;
  case R.id.myButton4:
   new AlertDialog.Builder(this).setTitle("单选框").setIcon(
     android.R.drawable.ic_dialog_info).setSingleChoiceItems(
     new String[] {"Item1", "Item2"}, 0,
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       dialog.dismiss();
      }
     }).setNegativeButton("取消", null).show();

   break;
   
   
   //mCameraView.tackPicture();
   //break;
  default:
   break;
  }
 }

 public static int ScreenOrient(Activity activity) {
  
  int orient = activity.getRequestedOrientation();
  if (orient != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && orient != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
   // 宽>高为横屏,反正为竖屏
   WindowManager windowManager = activity.getWindowManager();
   Display display = windowManager.getDefaultDisplay();
   int screenWidth = display.getWidth();
   int screenHeight = display.getHeight();
   orient = screenWidth < screenHeight ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
     : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  }
  return orient;
 }

 public static int curDregree(int cameraId) {
   android.hardware.Camera.CameraInfo info =
              new android.hardware.Camera.CameraInfo();
      android.hardware.Camera.getCameraInfo(cameraId, info);
      int rotation = activity.getWindowManager().getDefaultDisplay()
              .getRotation();
      int degrees = 0;
      switch (rotation) {
          case Surface.ROTATION_0: degrees = 0; break;
          case Surface.ROTATION_90: degrees = 90; break;
          case Surface.ROTATION_180: degrees = 180; break;
          case Surface.ROTATION_270: degrees = 270; break;
      }

      int result;
      if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
          result = (info.orientation + degrees) % 360;
          result = (360 - result) % 360;  // compensate the mirror
      } else {  // back-facing
          result = (info.orientation - degrees + 360) % 360;
      }
      return result;
 }
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  System.exit(0);
 }

}

 

错误如下(使用2.3.3模拟器运行):

04-09 07:26:21.834: D/AndroidRuntime(335): Shutting down VM


04-09 07:26:21.834: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)


04-09 07:26:21.855: E/AndroidRuntime(335): FATAL EXCEPTION: main


04-09 07:26:21.855: E/AndroidRuntime(335): java.lang.RuntimeException: Fail to connect to camera service


04-09 07:26:21.855: E/AndroidRuntime(335): at android.hardware.Camera.native_setup(Native Method)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.hardware.Camera.<init>(Camera.java:258)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.hardware.Camera.open(Camera.java:220)


04-09 07:26:21.855: E/AndroidRuntime(335): at com.cameraView.CameraView.surfaceCreated(CameraView.java:211)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.View.draw(View.java:6883)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.View.draw(View.java:6883)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.widget.FrameLayout.draw(FrameLayout.java:357)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.View.draw(View.java:6883)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.widget.FrameLayout.draw(FrameLayout.java:357)


04-09 07:26:21.855: E/AndroidRuntime(335): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewRoot.draw(ViewRoot.java:1522)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)


04-09 07:26:21.855: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)


04-09 07:26:21.855: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)


04-09 07:26:21.855: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)


04-09 07:26:21.855: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)


04-09 07:26:21.855: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)


04-09 07:26:21.855: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)


04-09 07:26:24.963: I/Process(335): Sending signal. PID: 335 SIG: 9


加载中
返回顶部
顶部