android if语句强制关闭

android if语句强制关闭,android,if-statement,media-player,Android,If Statement,Media Player,所以我有下面的代码。当我的应用程序中没有声音播放时,以下情况称为应用程序崩溃。据我所知,如果没有声音播放,它应该跳过if语句……那么为什么它会崩溃呢 public void IfSoundPlayingThenStop() if (currentSound.isPlaying()) { currentSound.release(); } 最简单的解决方案是,如果您不关心偶尔出现的空值,而希望忽略它们: if (currentSound != null &

所以我有下面的代码。当我的应用程序中没有声音播放时,以下情况称为应用程序崩溃。据我所知,如果没有声音播放,它应该跳过if语句……那么为什么它会崩溃呢

public void IfSoundPlayingThenStop()
if (currentSound.isPlaying())
    {
        currentSound.release();
    }

最简单的解决方案是,如果您不关心偶尔出现的空值,而希望忽略它们:

if (currentSound != null && currentSound.isPlaying())
    currentSound.release();

否则,在对变量进行任何其他使用之前,如果(currentSound==null)进行单独的
检查,并根据需要进行处理。

确保
currentSound
不为null。另外,添加一个logcat日志。我如何说notnull?它并没有以智能感知的方式表现出来。您可以在以下内容之前添加if语句:
if(currentSound==null){Log.d(“MYAPP”,“currentSound为null”}
Post异常堆栈trace@MByD感谢它起作用了!