下面的代码正在创建错误

Activity context;
context = new Activity();
try {
    InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
    //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

}catch (FileNotFoundException e) {
    Log.e(LOG_TAG, "File not found for recording ", e);
}


错误:

Error:(255, 87) error: unreported exception IOException; must be caught or declared to be thrown
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.


即使在包含try,catch块之后,为什么仍会产生错误?

最佳答案

像这样更改代码:

之前:

Activity context;
        context = new Activity();
        try {
            InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
            //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

        }catch (FileNotFoundException e) {
            Log.e(LOG_TAG, "File not found for recording ", e);
        }


后:

Activity context;
        context = new Activity();
        try {
            InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
            //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

        }catch (IOException e) {
            Log.e(LOG_TAG, "File not found for recording ", e);
        }

关于android - 即使包含try catch块,IO异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50599958/

10-10 22:16