真機運行崩潰,Logcat 僅顯示:
W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40e26438)
模擬器正常輸出完整堆棧。
部分廠商(華為、小米、OPPO、vivo 等)默認關閉 log 緩衝區 或限制第三方應用讀取系統日志,導致 android.util.Log 輸出被丟棄。
*#*#2846579#*#* → ProjectMenu → 1.Background Setting → 3.Log Setting → 勾選所有 Log 開關。Application 類或 Thread.setDefaultUncaughtExceptionHandler 中將異常寫入文件(getExternalFilesDir(null)),不依賴 logcat。Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
File log = new File(getExternalFilesDir(null), "crash_" + System.currentTimeMillis() + ".log");
try (PrintWriter pw = new PrintWriter(log)) {
e.printStackTrace(pw);
} catch (IOException ignored) {}
// 可選:上傳到服務器
});開啟日志開關後重現崩潰,Logcat 應顯示完整 E/AndroidRuntime: FATAL EXCEPTION: main 堆棧。