这是例外:
Attempt to invoke virtual method
android.hardware.camera2.CaptureRequest$Builder
android.hardware.camera2.CameraDevice.createCaptureRequest(int)' on a
null object reference
我已经把---->箭头指向似乎导致异常的代码行,当我在应用程序上按记录时会发生此异常
如果您需要更多信息,请稍微询问,我会发布所有相关信息,我试图获得Android开发的所有帮助,谢谢!!!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera2_video_image2);
createVideoFolder();
mMediaRecorder = new MediaRecorder();
mTextureView = (TextureView) findViewById(R.id.textureView);
mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton);
mRecordImageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(mIsRecording){
mIsRecording = false;
mRecordImageButton.setImageResource(R.mipmap.btn_video_online);
//stop recording then restart preview
mMediaRecorder.stop();
mMediaRecorder.reset();
startPreview();
}else{
------> checkWriteStoragePermission();
mIsRecording = true;
mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
}
}
});
}
private void checkWriteStoragePermission() {
//check if it is Marshmallow build
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
{
mIsRecording = true;
mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
try{
createVideoFileName();
}catch(IOException e){
e.printStackTrace();
}
------> startRecord();
mMediaRecorder.start();
}else{
if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(this,"app needs to able to save videos",Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_STORAGE_PERMISSION_RESULT);
}
}else{
mIsRecording = true;
mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
try{
createVideoFileName();
}catch(IOException e){
e.printStackTrace();
}
startRecord();
mMediaRecorder.start();
}
}
private void startRecord(){
try {
setupMediaRecorder();
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
Surface recordSurface = mMediaRecorder.getSurface();
try
----> {mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
mCaptureRequestBuilder.addTarget(previewSurface);
mCaptureRequestBuilder.addTarget(recordSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
session.setRepeatingRequest(mCaptureRequestBuilder.build(),null,null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
catch(IOException e){
e.printStackTrace();
}
}
完整堆栈跟踪:
FATAL EXCEPTION: main
Process: com.example.tadewos.myapplication, PID: 8049
java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.camera2.CaptureRequest$Builder android.hardware.camera2.CameraDevice.createCaptureRequest(int)' on a null object reference
at com.example.tadewos.myapplication.Camera2VideoImageActivity2.startRecord(Camera2VideoImageActivity2.java:309)
at com.example.tadewos.myapplication.Camera2VideoImageActivity2.checkWriteStoragePermission(Camera2VideoImageActivity2.java:450)
at com.example.tadewos.myapplication.Camera2VideoImageActivity2.access$900(Camera2VideoImageActivity2.java:48)
at com.example.tadewos.myapplication.Camera2VideoImageActivity2$3.onClick(Camera2VideoImageActivity2.java:165)
at android.view.View.performClick(View.java:5721)
at android.view.View$PerformClick.run(View.java:22624)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是我声明Cameradevice的地方:
private static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;
private static final int REQUEST_STORAGE_PERMISSION_RESULT = 1;
private TextureView mTextureView;
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener(){
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
setupCamera(width, height);
connectCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
private CameraDevice mCameraDevice;
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback(){
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
if(mIsRecording){
try {
createVideoFileName();
} catch (IOException e) {
e.printStackTrace();
}
startRecord();
mMediaRecorder.start();
}else{
startPreview();}
// Toast.makeText(getApplicationContext(),
// "Camera connection made!", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice camera, int error) {
camera.close();
mCameraDevice = null;
}
};