我正在使用本教程:
http://www.vogella.com/articles/AndroidCamera/article.html
捕获图像并将其保存在 SD 卡上,但是每当我运行此程序时,都会得到:不幸的是应用程序已停止
错误说:
引起:java.lang.reflect.InvocationTargetException
第 39 行是:
camera.takePicture(null, null, new PhotoHandler(getApplicationContext()));
日志输出:
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();
}