[Android] RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Tadashi Shigeoka ·  Tue, June 25, 2013

The Android app I was developing crashed with the following error.

RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

The cause was that I was performing UI-related operations inside doInBackground(). (Specifically, I was trying to display an error message with Toast)

When an error occurs, you should not display error dialogs or similar inside doInBackground(), but instead return null and handle error notifications appropriately in the onPostExecute method.

The doInBackground method runs on a thread obtained from a separate pool from the UI thread, so GUI operations must not be performed. Instead, GUI operations can be completed within the onProgressUpdate or onPostExecute methods, which are synchronized with the UI thread.

[Quote from]: バックグラウンドスレッドでダイアログを生成してはいけない - Kazzzの日記

[References]

Android Can’t create handler inside thread that has not called Looper.prepare() - Stack Overflow

android - Can’t create handler inside thread that has not called Looper.prepare() - Stack Overflow

That’s all from the Gemba.