Android 而try catch块中的循环导致应用程序崩溃
我的线程的run()方法在try-catch块中具有while循环,如下所示:Android 而try catch块中的循环导致应用程序崩溃,android,multithreading,while-loop,try-catch,Android,Multithreading,While Loop,Try Catch,我的线程的run()方法在try-catch块中具有while循环,如下所示: try{ while(true){ // some code here if(condition) break; else //more code here } }catch(Exception e){..} 我猜代码没有进入无限循环,因为: 我的if条件保证在一些迭代之后导致循环中断 由于c
try{
while(true){
// some code here
if(condition)
break;
else
//more code here
}
}catch(Exception e){..}
我猜代码没有进入无限循环,因为:
private static final int AUDIO_SOURCE=MediaRecorder.AudioSource.MIC;
private static final int SAMPLE_RATE_IN_HZ=44100;
private static final int CHANNEL_CONFIG=AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_FORMAT=AudioFormat.ENCODING_PCM_16BIT;
public void run() {
//writing to AudioTrack object using 512kB buffer
int buffSize=512*1024; //512kb = 512*1024 B
byte[] buff=new byte[buffSize];
int fileSize=(int)outputFile.length(); //outputFile=.PCM file
int bytesRead=0,readCount=0;
FileInputStream fin=null;
try {
fin = new FileInputStream(outputFile);
}catch(Exception e){}
int TrackBufferSizeInBytes=android.media.AudioTrack.getMinBufferSize(SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG,AUDIO_FORMAT);
//create AudioTrack object
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,SAMPLE_RATE_IN_HZ,CHANNEL_CONFIG,
AUDIO_FORMAT, TrackBufferSizeInBytes, AudioTrack.MODE_STREAM);
at.play();
try{
while(bytesRead<fileSize){
readCount=fin.read(buff,0,buffSize);
if(readCount==(-1)) // if EOF is reached
break;
else{
at.write(buff, 0, readCount); //write read bytes to Track
bytesRead+=readCount;
}
}
at.stop();
at.release();
at=null;
fin.close();
}catch(Exception e){}
}
private static final int AUDIO\u SOURCE=MediaRecorder.AudioSource.MIC;
专用静态最终整数采样率(单位:HZ)=44100;
专用静态最终int通道配置=单声道中的AudioFormat.CHANNEL;
专用静态最终整数音频格式=AudioFormat.ENCODING\u PCM\u 16位;
公开募捐{
//使用512kB缓冲区写入AudioTrack对象
int buffSize=512*1024;//512kb=512*1024 B
字节[]buff=新字节[buffSize];
int fileSize=(int)outputFile.length();//outputFile=.PCM文件
int bytesRead=0,readCount=0;
FileInputStream fin=null;
试一试{
fin=新文件输入流(输出文件);
}捕获(例外e){}
int TrackBufferSizeInBytes=android.media.AudioTrack.getMinBufferSize(采样率,频率,频道配置,音频格式);
//创建AudioTrack对象
AudioTrack at=新的AudioTrack(AudioManager.STREAM音乐、采样率、频道配置、,
音频_格式、TrackBufferSizeInBytes、AudioTrack.MODE_流);
at.play();
试一试{
(bytesRead您应该在catch块中使用printStackTrace()方法,它将显示在哪一行代码上生成的异常
尝试在catch块中使用此选项:
e.printStackTrace();
或者您可以插入断点并调试代码以查看其行为
或者你也可以在Logcat中使用和查看变量的值如果捕获到异常,直接进入catch块,你可以忘记你的循环。你捕获的是什么异常?你能在catch中记录.e(“TAG”,“err”,e);你是否使用了大量内存或递归?“应用程序崩溃”-这表明日志中会出现异常。这是什么?@VM:循环体有read()和write()操作。@thumbmunkeys:不只是一个read和write语句。作为提示,使用完整单词的答案比txtspeak更容易阅读…所以用“you”代替“u”等等@VivekWarde:是的,这很好,但我不能使用emulator,因为我的应用程序需要硬件资源来录制和播放声音。@akshay7692好的,你可能在使用设备,但在你的代码中你没有处理异常&我想你没有调试你的代码,至少要确定应用程序在哪一行代码崩溃!@VivekWarde:嗯,每次我在Eclipse中调试我的代码,emulator启动了。有没有办法在不启动emulator的情况下调试它?可以使用一个设备&有关更多信息,请阅读此