2011-05-25 89 views
0

所以我有以下代碼。當我的應用程序中沒有播放聲音時,下面的內容被稱爲應用程序崩潰。據我所知,如果沒有聲音播放,它應該跳過if語句...那麼它爲什麼會崩潰?android如果聲明強制關閉

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

確保'currentSound'不爲空。另外,添加一個logcat日誌。 – MByD 2011-05-25 08:15:29

+0

我怎麼說不是空?它沒有顯示在智能感知 – karlstackoverflow 2011-05-25 08:17:15

+2

許多方法來做到這一點。 if(currentSound == null){Log.d(「MYAPP」,「currentSound is null」)' – MByD 2011-05-25 08:18:49

回答

1

最簡單可行的解決方案,如果你只是不關心,偶爾空寧可忽略它們:

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

否則,做任何其他使用前做一個單獨的if(currentSound == null)檢查變量,並根據需要處理事物。