Audio整合后测试出错,希望hyman老师和同学们看下
在audio整合章节的测试,log抛出一个异常,
异常1 E/InputEventReceiver(17950): Exception dispatching input event. E/MessageQueue-JNI(17950): Exception in MessageQueue callback: handleReceiveCallback java.lang.NullPointerException at com.imoc_recorder.view.AudioManager.release(AudioManager.java:98) at com.imoc_recorder.view.AudioManager.cancel(AudioManager.java:103) at com.imoc_recorder.view.AudioRecorderButton.onTouchEvent(AudioRecorderButton.java:154) 异常2 /AndroidRuntime(17950): FATAL EXCEPTION: main E/AndroidRuntime(17950): java.lang.NullPointerException at com.imoc_recorder.view.AudioManager.release(AudioManager.java:98) at com.imoc_recorder.view.AudioManager.cancel(AudioManager.java:103) at com.imoc_recorder.view.AudioRecorderButton.onTouchEvent(AudioRecorderButton.java:154) 两个异常都是在同一个地方出错 AudioRecorderButton.java->onTouchEvent(MotionEvent event)->if (!isRecording||mTime<0.6f) ->mAudioManager.cancel();->release();->mMediaRecorder.stop(); 我上网查了下没发现什么错误,希望老师和同学们指点,为什么会错,跟老师的代码一样阿 其实我是长按,不知道为什么会进到这里 public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); int x = (int) event.getX(); int y = (int) event.getY(); // try { switch (action) { case MotionEvent.ACTION_DOWN: changeState(STATE_RECORDING); break; case MotionEvent.ACTION_MOVE: if (isRecording) { if (wantToCancel(x,y)) { changeState(STATE_WANT_TO_CANCEL); }else { changeState(STATE_RECORDING); } } break; case MotionEvent.ACTION_UP: if (!mReady) { reset(); return super.onTouchEvent(event); } if (!isRecording||mTime<0.6f) {//时间短,还没有来得及repared mDailogManager.tooShort(); mAudioManager.cancel(); mHandler.sendEmptyMessageDelayed(MSG_DIALOG_DIMISS, 1300);//1.3秒后关闭tooshort }else if (mCurState==STATE_RECORDING) {//正常录制结束 //release mDailogManager.dimissDialog(); mAudioManager.release(); //callbackToAct if (mListener != null) { mListener.onFinish(mTime, mAudioManager.getCurrentFilePath()); } }else if (mCurState==STATE_WANT_TO_CANCEL) { //cancel mDailogManager.dimissDialog(); mAudioManager.cancel(); } reset(); break; public void release() { mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder = null; } public void cancel() { release(); if (mCurrentFilePath != null) { File file = new File(mCurrentFilePath); file.delete(); mCurrentFilePath = null; } }