android 停止录制时出现 NullPointerException
Posted
技术标签:
【中文标题】android 停止录制时出现 NullPointerException【英文标题】:NullPointerException when recording is stopped android 【发布时间】:2014-04-05 11:07:05 【问题描述】:我正在尝试制作一个在单击按钮时开始录制的应用程序,当再次单击时它会停止录制。我认为我在录制时没有问题,但是当我再次单击该按钮时,应用程序会因 NullPointerException 而崩溃,并且据说在无效状态 1 下调用 stop
我的代码:
package com.example.victwo;
import java.io.IOException;
import android.media.MediaRecorder;
public class Audio
private String path;
MediaRecorder myAudioRecorder = null;
public Audio(String path)
this.path = path;
public void play()
public void startRecording()
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudiosource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(path);
try
myAudioRecorder.prepare();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
myAudioRecorder.start();
public void stopRecording()
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
这是我调用音频类方法的活动的代码:
case R.id.action_record:
Audio recAndPlay = new Audio(this.path + "/" + this.title);
if(flag == 0)
Log.d("RECORD", "START");
flag = 1;
if(title.equals(""))
setTitle();
recAndPlay.startRecording();
return true;
if(flag == 1)
Log.d("RECORD", "STOP");
recAndPlay.stopRecording();
return true;
日志猫:
04-05 14:02:58.909: E/AndroidRuntime(5730): FATAL EXCEPTION: main
04-05 14:02:58.909: E/AndroidRuntime(5730): java.lang.NullPointerException
04-05 14:02:58.909: E/AndroidRuntime(5730): at com.example.victwo.Audio.stopRecording(Audio.java:41)
04-05 14:02:58.909: E/AndroidRuntime(5730): at com.example.victwo.NoteActivity.onOptionsItemSelected(NoteActivity.java:158)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.app.Activity.onMenuItemSelected(Activity.java:2205)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:339)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:344)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:777)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:922)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:544)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:105)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.view.View.performClick(View.java:2485)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.view.View$PerformClick.run(View.java:9080)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.os.Handler.handleCallback(Handler.java:587)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.os.Looper.loop(Looper.java:123)
04-05 14:02:58.909: E/AndroidRuntime(5730): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-05 14:02:58.909: E/AndroidRuntime(5730): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:02:58.909: E/AndroidRuntime(5730): at java.lang.reflect.Method.invoke(Method.java:507)
04-05 14:02:58.909: E/AndroidRuntime(5730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-05 14:02:58.909: E/AndroidRuntime(5730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-05 14:02:58.909: E/AndroidRuntime(5730): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
NoteActivity.java
中的第 158 行是什么
您发布的代码看起来不错。也许这是您处理 Audio 类对象的方式?也许开始和停止录制的按钮的侦听器在每次单击时都会创建新对象,因此 stopRecording 是在没有提前 startRecording() 的情况下调用的?
【参考方案1】:
在 startRecording() 之外初始化您的媒体播放器
myAudioRecorder = new MediaRecorder();
像下面这样修改你的代码
public class Audio
private String path;
MediaRecorder myAudioRecorder = new MediaRecorder();
public Audio(String path)
this.path = path;
public void play()
public void startRecording()
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(path);
try
myAudioRecorder.prepare();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
myAudioRecorder.start();
public void stopRecording()
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
希望对你有帮助
【讨论】:
当我在方法外初始化 myAudioRecorder 时,给出:04-05 15:16:54.630: E/AndroidRuntime(8569): FATAL EXCEPTION: main 04-05 15:16:54.630: E /AndroidRuntime(8569): java.lang.IllegalStateException 04-05 15:16:54.630: E/AndroidRuntime(8569): at android.media.MediaRecorder.stop(Native Method)【参考方案2】:在您调用 stopRecording()
时,myAudioRecorder
必须为空。我无法从您的代码中判断为什么(您是否两次致电stopRecording()
?)。如果您发布更多代码,我可能会为您提供帮助。您还可以在 stopRecording()
method 中的语句加上一个检查:
public void stopRecording()
if(myAudioRecorder != null)
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
【讨论】:
【参考方案3】:private void stopRecording()
if (null != recorder)
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
【讨论】:
如果它解释了它是如何回答问题的,这将更加有用。仅仅提供一段代码可能无法帮助提问者或其他任何人理解最初的问题。 在您使用此方法的地方发布您的活动代码,我会尽力提供帮助【参考方案4】:请在停止之前检查 MediaRecorder 对象是否不为空。
【讨论】:
【参考方案5】:我认为您采取的方法不正确。为什么一开始就将媒体播放器声明为空。首先初始化,完成后使用媒体播放器类的方法来释放/重置它。
在您开始使用媒体播放器之前,请使用 if()... 块来检查它是否已初始化。下面的例子;
private void stopRecording()
if (myAudioRecorder != null)
myAudioRecorder.stop();
myAudioRecorder.reset();
myAudioRecorder.release();
myAudioRecorder = null;
【讨论】:
据说stop在无效状态1被调用 在您使用此方法的地方发布您的活动代码,我会尽力提供帮助【参考方案6】:另一个原因可能是,它已经停止了,然后您正在重新使用 .stop()
函数,这会导致问题,请检查您的逻辑和代码...
【讨论】:
以上是关于android 停止录制时出现 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
调用 flash externalInterface 回调时出现 SCRIPT 16389 错误
从 Xcode 6 重新构建应用程序时出现 OSStatus 错误 2003334207