我看过另一个类似的帖子并尝试了他们的建议,但同样的问题发生了,所以我不想劫持他们的帖子,所以我发布了一个不同的帖子。对不起。
无论如何,我正在尝试使用 Eclipse 和 Android 4.2.2 api 创建一个相机应用程序。每当我在我的设备上调试或运行应用程序时,我都会得到java.lang.IllegalStateException
. 我很高兴有人可以在这里查看这是我的 logcat 跟踪:
06-02 15:35:32.069: W/dalvikvm(12329): threadid=1: thread exiting with uncaught exception (group=0x40d46930)
06-02 15:35:32.069: E/AndroidRuntime(12329): FATAL EXCEPTION: main
06-02 15:35:32.069: E/AndroidRuntime(12329): java.lang.IllegalStateException: Could not execute method of the activity
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.view.View$1.onClick(View.java:3599)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.view.View.performClick(View.java:4204)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.view.View$PerformClick.run(View.java:17355)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.os.Handler.handleCallback(Handler.java:725)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.os.Looper.loop(Looper.java:137)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-02 15:35:32.069: E/AndroidRuntime(12329): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 15:35:32.069: E/AndroidRuntime(12329): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 15:35:32.069: E/AndroidRuntime(12329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-02 15:35:32.069: E/AndroidRuntime(12329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-02 15:35:32.069: E/AndroidRuntime(12329): at dalvik.system.NativeStart.main(Native Method)
06-02 15:35:32.069: E/AndroidRuntime(12329): Caused by: java.lang.reflect.InvocationTargetException
06-02 15:35:32.069: E/AndroidRuntime(12329): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 15:35:32.069: E/AndroidRuntime(12329): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.view.View$1.onClick(View.java:3594)
06-02 15:35:32.069: E/AndroidRuntime(12329): ... 11 more
06-02 15:35:32.069: E/AndroidRuntime(12329): Caused by: java.lang.RuntimeException: takePicture failed
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.hardware.Camera.native_takePicture(Native Method)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.hardware.Camera.takePicture(Camera.java:1095)
06-02 15:35:32.069: E/AndroidRuntime(12329): at android.hardware.Camera.takePicture(Camera.java:1040)
06-02 15:35:32.069: E/AndroidRuntime(12329): at com.example.de.vogella.camera.api.MakePhotoActivity.onClick(MakePhotoActivity.java:46)
06-02 15:35:32.069: E/AndroidRuntime(12329): ... 14 more
我在该onClick()
方法中尝试了以下代码,但在尝试使用该应用程序时仍然遇到相同的错误:
if(camera == null)
{
// Warn user that camera is not available via "Toast" or similar.
} else {
camera.takePicture(null, null,new PhotoHandler(getApplicationContext()))
}
我还尝试了以下方法。我mPhotoHandler
在类的顶部声明了我保留其他变量的方法,并且在onClick()
方法中我只放入了这段代码:
mPhotoHandler = new PhotoHandler(getApplicationContext());
camera.takePicture(null, null,mPhotoHandler);
这是我的onPause()
方法的代码:
protected void onPause() {
if (camera != null) {
camera.release();
camera = null;
}
super.onPause();
}
上面的代码有问题吗?请告诉我。