未处理的异常 java.io.iOException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30587997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unhandled Exception java.io.iOException
提问by steve 1
I am getting unhandled exception java.io.IOException
at compile time. I have posted the code below and pointed out the error line. I searched many post regarding to this issue but I don't get any relevant solution.
我java.io.IOException
在编译时遇到未处理的异常。我已经发布了下面的代码并指出了错误行。我搜索了很多关于这个问题的帖子,但我没有得到任何相关的解决方案。
MainActivity.java:
主活动.java:
public class MainActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
...................
}
public void startRecording() throws IOException {
..................
}
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (RecordFile != null && ifexist.equals("true")) {
}
else {
if (isRecclicked == true) {
recordButtonPressed = true;
try {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 2 seconds
startRecording(); ----> Unhandled Exception java.io.iOException
}
}, 2000);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
回答by User404
try {
startRecording();
}catch(IOException ex) {
//Do something with the exception
}
回答by Jake
You call startRecording()
, which, I assume, can throw an IOException
, but you didn't specify that an IOException
can occur inside public void run()
. It should be fixed if you surround startRecording()
with a try-catch block.
您调用startRecording()
,我假设它可以抛出一个IOException
,但您没有指定 anIOException
可以出现在public void run()
. 如果你startRecording()
用 try-catch 块包围它应该是固定的。