java - 尽管变量已初始化,但我得到可能未初始化错误

标签 java android

我在我正在开发的 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/

相关文章:

java - 如何将AWS SDK添加到OSGi环境

java - 应用程序版本更新后,我可以在哪里存储用户数据以保留这些数据?

Android软键盘问题

android - 如何添加 Firebase 对象/节点+子树修改时的时间戳?

android - 使用 Proguard 剥离未使用的支持库类

Android Studio lint_baseline.xml 不排除很多它应该解决的问题

java - 简单日期格式解析异常 : Unparseable date Error

java - 即使使用@transactional注释后,如何在spring中解析 'No transaction is currently active'?

java - 最后尝试关闭多个流

android - 如何确保一个 android Activity 仅在 backstack 中驻留一次