為何出現此錯誤訊息?Suspicious indentation: This is indented but is not continuing the previous expression (backgroundStartTime ...) (Previous statement here)

 你遇到的錯誤是由於不正確的縮排(indentation)引起的。當然,這樣的錯誤可能會讓程式解析器無法識別你的代碼邏輯。

在 Kotlin 中,縮排非常重要,正確的縮排能確保代碼的邏輯清晰明了。根據你的錯誤訊息,似乎是 backgroundStartTime = System.currentTimeMillis() 這一行縮排不正確。這行代碼應該與 super.onDestroy() 同級,或者應該放在適當的程式邏輯塊內。

修正方案:

請確保所有代碼行的縮排一致,且正確結構。根據你的邏輯,下面是正確的寫法:

kotlin
override fun onDestroy() { super.onDestroy() // 正確的縮排 backgroundStartTime = System.currentTimeMillis() // 正常退出應用程式 android.os.Process.killProcess(android.os.Process.myPid()) exitProcess(0) }

縮排解釋:

  • super.onDestroy() 是首先執行的,縮排為 0 層(最外層)。

  • backgroundStartTime = System.currentTimeMillis() 需要位於 onDestroy() 內,所以它的縮排應該是 1 層,與 super.onDestroy() 在同一層級。

  • android.os.Process.killProcess()exitProcess(0) 也是在 onDestroy() 方法內部,所以它們的縮排應該是與 backgroundStartTime 一致。

這樣就能避免不正確的縮排導致的錯誤。

沒有留言:

張貼留言

excel 最大值用max,取最小值但要排除0,要怎麼寫?

 =MIN(IF(A1:A10<>0, A1:A10))