takepicture()给出了Nullpointer异常

时间:2013-02-07 17:30:00

标签: android nullpointerexception android-camera

我正在构建一个使用前后两个相机的相机应用程序,但在此语句中我得到一个空指针错误。

public void onClick(查看视图){

  //camera.takePicture(shutter, raw, postview, jpeg)
  //camera.takePicture(null, null, new PhotoHandler(getApplicationContext()));
camera.takePicture(null, null,
    new PhotoHandler(MakePhotoActivity.this));

}

请帮助我,因为我知道问题就在这里。

提前致谢。

02-07 23:06:12.445: E/AndroidRuntime(30045): FATAL EXCEPTION: main
02-07 23:06:12.445: E/AndroidRuntime(30045): java.lang.IllegalStateException: Could not execute method of the activity
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.view.View$1.onClick(View.java:2144)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.view.View.performClick(View.java:2485)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.view.View$PerformClick.run(View.java:9080)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.os.Handler.handleCallback(Handler.java:587)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.os.Looper.loop(Looper.java:130)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at java.lang.reflect.Method.invoke(Method.java:507)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at dalvik.system.NativeStart.main(Native Method)
02-07 23:06:12.445: E/AndroidRuntime(30045): Caused by: java.lang.reflect.InvocationTargetException
02-07 23:06:12.445: E/AndroidRuntime(30045):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at java.lang.reflect.Method.invoke(Method.java:507)
02-07 23:06:12.445: E/AndroidRuntime(30045):    at android.view.View$1.onClick(View.java:2139)
02-07 23:06:12.445: E/AndroidRuntime(30045):    ... 11 more
02-07 23:06:12.445: E/AndroidRuntime(30045): Caused by: java.lang.NullPointerException
02-07 23:06:12.445: E/AndroidRuntime(30045):    at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:41)

2 个答案:

答案 0 :(得分:0)

将“getApplicationContext()”更改为“ActivityClassName.this”

答案 1 :(得分:0)

我在我的应用程序中使用了一个similer代码,我的应用程序也习惯于在行上抛出空指针异常:

camera.takePicture(null, null,new PhotoHandler(getApplicationContext()));

我认为这是由于相机获得空值引起的。为了避免这种异常,我对这行代码进行了检查。所以onClick函数看起来像:

public void onClick(View view) {
  if (camera != null) {
    Log.d("camera state","camera is NOT null");  
  }else{
    Log.d("camera state","camera is null");
    camera = android.hardware.Camera.open(cameraId);
  }
  camera.takePicture(null, null,new PhotoHandler(getApplicationContext()));
}

ps:我的代码是为了使用前置凸轮编写的。希望它有帮助!