我正在尝试使用我的应用程序录制视频,但是当我点击“重新连线”按钮时,它会在Camera.Parameters params = mCamera.getParameters();
上抛出nullpointer异常。我搜索了很多,但找到了一个像camera.open()
这样的解决方案但是在我的情况下也没有工作。我的手机有后置摄像头,所以我也尝试了camera.open(0)或camera.open(1),但没有任何帮助。
这是我的代码供您参考。
@Override
public void onCreate(Bundle savedInstanceState) {
//mCamera = Camera.open(0);
if (mCamera == null) {
mCamera = getCameraInstance();
// Create preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
} else {
mCamera.release();
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
}
int i = R.id.videoFrameView;
Object o = this.findViewById(i);
final FrameLayout preview = (FrameLayout) o;
preview.addView(mPreview);
//mCamera = Camera.open(1);
Camera.Parameters params = mCamera.getParameters();
// Here i am getting nullpointer exception.
params.set("cam_mode", 1);
mCamera.setParameters(params);
sizes = params.getSupportedPreviewSizes();
这是我的logacat。
04-23 19:21:27.642: E/AndroidRuntime(25598): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aml.videoaml/com.aml.videoaml.RecordActivity}: java.lang.NullPointerException
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.ActivityThread.access$600(ActivityThread.java:134)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.os.Looper.loop(Looper.java:154)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.ActivityThread.main(ActivityThread.java:4624)
04-23 19:21:27.642: E/AndroidRuntime(25598): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 19:21:27.642: E/AndroidRuntime(25598): at java.lang.reflect.Method.invoke(Method.java:511)
04-23 19:21:27.642: E/AndroidRuntime(25598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
04-23 19:21:27.642: E/AndroidRuntime(25598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
04-23 19:21:27.642: E/AndroidRuntime(25598): at dalvik.system.NativeStart.main(Native Method)
04-23 19:21:27.642: E/AndroidRuntime(25598): Caused by: java.lang.NullPointerException
04-23 19:21:27.642: E/AndroidRuntime(25598): at com.aml.videoaml.RecordActivity.onCreate(RecordActivity.java:63)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.Activity.performCreate(Activity.java:4479)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
04-23 19:21:27.642: E/AndroidRuntime(25598): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
04-23 19:21:27.642: E/AndroidRuntime(25598):
我不明白我的代码中有什么问题,请给我任何参考或提示 谢谢
答案 0 :(得分:0)
如果由于某种原因或应用程序崩溃而未释放相机,则可能无法打开相机。我不确定你的情况,但你也可以考虑这种可能性。在这种情况下,重启设备将起作用。