引起:java.lang.reflect.InvocationTargetException
Posted
技术标签:
【中文标题】引起:java.lang.reflect.InvocationTargetException【英文标题】:Caused by: java.lang.reflect.InvocationTargetException 【发布时间】:2013-05-24 14:31:18 【问题描述】:我正在使用本教程:
http://www.vogella.com/articles/androidCamera/article.html
捕获图像并将其保存在 SD 卡上,但是每当我运行此程序时,都会出现:不幸的是应用程序已停止
错误提示:
原因: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();
【问题讨论】:
@SilentKiller 你好,已编辑 发布您的 MakePhotoActivity 无法执行活动方法意味着一些东西尚未初始化。 @RajithaSiriwardena 我已粘贴 检查你的相机变量是否为空 【参考方案1】:要打开 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();
// ...
最后,在您的清单文件中,为相机添加一个“Uses-Permission”。
<uses-permission android:name="android.permission.CAMERA" />
:更新答案:
用于自定义相机
点击此链接获取自定义相机:
-
Custom Camera Application
Custom camera for Android created for testing, playing and learning
How to capture and save an image using custom camera in Android?
How to create a custom layout for your camera in Android?
【讨论】:
2) 为测试、游戏和学习而创建的 Android 自定义相机 这个例子帮助我解决了这个问题。我指的是CameraPreview 类的surfaceChanged 方法【参考方案2】:您通过使用反射调用方法添加了额外的抽象级别。反射层将任何异常包装在InvocationTargetException
中,这使您可以区分实际由反射调用失败(例如,您的参数列表无效)引起的异常和调用的方法中的失败.
只需解开 InvocationTargetException
中的原因,您就会找到原来的原因。
【讨论】:
【参考方案3】:我认为你的问题在这里:
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 行。
【讨论】:
希望我可以评论其他人的问题。我没有足够的代表。【参考方案4】:您的camera
对象似乎为空
【讨论】:
【参考方案5】:您的 AVD 上应该需要摄像头所以编辑您的 AVD 并在背面和正面添加摄像头.....
【讨论】:
以上是关于引起:java.lang.reflect.InvocationTargetException的主要内容,如果未能解决你的问题,请参考以下文章
csharp 可能会引起问题的类的继承问题,父类后来增加的方法可能会引起子类的函数重载错误