java.lang.RuntimeException: An error occured while executing doInBackground()

后端 未结 4 622
[愿得一人]
[愿得一人] 2020-12-03 21:27

Sometimes i get this crash error in my app

java.lang.RuntimeException: An error occured while executing doInBackground()

This is the full l

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 22:25

    You can't do any GUI work on a background thread. Instead you should post a Runnable to a handler created in the GUI thread to make your Toast execute on the correct thread.

    EDIT: Since the homework tag was removed, I'll give an outline how to do it;

    • new() up a Handler in your GUI thread (onCreate is a good place) and save it in a member variable, say "handler".
    • Move showing your Toast to a Runnable (anonymous class is pretty good for this)
    • To show the toast, do a handler.post() with your Runnable as a parameter. The Runnable will execute in the GUI thread.

    For a complete example, see this article.

提交回复
热议问题