引起:java.lang.reflect.InvocationTargetException

时间:2013-05-29 10:57:53

标签: android android-camera

我正在使用本教程:

http://www.vogella.com/articles/AndroidCamera/article.html

捕获图像并将其保存在SD卡上,但每当我运行此程序时,获取:不幸的是App已停止

错误说:

引起:java.lang.reflect.InvocationTargetException

第39行是:

camera.takePicture(null,null,         新的PhotoHandler(getApplicationContext()));

Logcat输出:

05-29 16:19:38.351: E/AndroidRuntime(767): FATAL EXCEPTION: main
05-29 16:19:38.351: E/AndroidRuntime(767): java.lang.IllegalStateException: Could not execute method of the activity
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3599)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View.performClick(View.java:4204)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$PerformClick.run(View.java:17355)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Handler.handleCallback(Handler.java:725)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:137)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-29 16:19:38.351: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767): Caused by: java.lang.reflect.InvocationTargetException
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3594)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 11 more
05-29 16:19:38.351: E/AndroidRuntime(767): Caused by: java.lang.NullPointerException
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:39)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 14 more

MakePhotoActivity.java:

  public class MakePhotoActivity extends Activity {
  final static String DEBUG_TAG = "MakePhotoActivity";
  private Camera camera;
  private int cameraId = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // do we have a camera?
    if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
          .show();
    } else {
      cameraId = findFrontFacingCamera();
      if (cameraId < 0) {
        Toast.makeText(this, "No front facing camera found.",
            Toast.LENGTH_LONG).show();
      } else {
        camera = Camera.open(cameraId);
      }
    }
  }

  public void onClick(View view) {
    camera.takePicture(null, null,
        new PhotoHandler(getApplicationContext()));
  }

  private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }

  @Override
  protected void onPause() {
    if (camera != null) {
      camera.release();
      camera = null;
    }
    super.onPause();
  }

5 个答案:

答案 0 :(得分:5)

要打开Android默认相机,您可以编写简单的2行代码:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

要获取所点击图片的路径,您需要覆盖onActivityResult()。以下代码可帮助您获取所单击图像的路径:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    String result = data.toURI();
    // ...
  }
}

最后,在您的清单文件中,添加&#34; Uses-Permission&#34;对于相机。

<uses-permission android:name="android.permission.CAMERA" />

:更新后的答案:

对于Custom Camra

点击自定义相机的链接:

  1. Custom Camera Application
  2. Custom camera for Android created for testing, playing and learning
  3. How to capture and save an image using custom camera in Android?
  4. How to create a custom layout for your camera in Android?

答案 1 :(得分:2)

通过使用反射调用方法,您已经添加了额外的抽象级别。反射层包装InvocationTargetException中的任何异常,这使您可以区分实际由反射调用中的失败引起的异常(例如,您的参数列表无效)和内部失败。方法叫做。

只需打开InvocationTargetException内的原因,您就可以获得原始版本。

答案 2 :(得分:0)

我认为你的问题在这里:

Caused by: java.lang.NullPointerException
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:39)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 14 more

检查出什么问题@ MakePhotoActivity.java,第39行。

答案 3 :(得分:0)

似乎您的camera对象为空

答案 4 :(得分:-3)

您应该需要AVD上的相机所以编辑您的AVD并在背面和前面添加相机......