我在我正在开发的 Android 应用程序中遇到以下代码问题。现在,即使我已经初始化了变量,我也收到错误变量可能尚未初始化。
Activity context;
context = new Activity();
SimpleSpkDetSystem alizeSystem;
try {
InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
try {
alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());
} catch (AlizeException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
configAsset.close();
} catch (java.io.IOException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
InputStream backgroundModelAsset;
try {
backgroundModelAsset = context.getApplicationContext().getAssets().open("gmm/world.gmm");
} catch (java.io.IOException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
try {
alizeSystem.loadBackgroundModel(backgroundModelAsset);
} catch (AlizeException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
try {
backgroundModelAsset.close();
} catch (java.io.IOException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
以下是我遇到的错误
Error:(274, 13) error: variable alizeSystem might not have been initialized
Error:(274, 45) error: variable backgroundModelAsset might not have been initialized
Error:(279, 9) error: variable backgroundModelAsset might not have been initialized
Error:(274, 44) error: unreported exception IOException; must be caught or declared to be thrown
最佳答案
Now even though I have initialized the variables
但你还没有 - 不一定。假设 new SimpleSpkDetSystem(...)
调用抛出 AlizeException
。您正在捕获该异常并继续 - 但您不会为 alizeSystem
分配值。对于所有其他变量也是如此。
我怀疑这里的解决方案不只是记录异常然后继续,就好像它们没有发生一样,而是让它们冒泡给调用者。
关于java - 尽管变量已初始化,但我得到可能未初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600404/