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

依然范特西╮ 提交于 2019-12-20 02:52:08

问题


I'm trying to add ScoreNinja, the global high score system, to my Android game, and it works fine when I load it on my phone, but when I release it into the wild, I got crash reports saying:

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

Here is part of the call stack:

android.os.Handler.<init>(Handler.java:121)
android.app.Dialog.<init>(Dialog.java:99)
android.app.AlertDialog.<init>(AlertDialog.java:65)
android.app.AlertDialog.<init>(AlertDialog.java:61)
android.app.AlertDialog$Builder.create(AlertDialog.java:797)
android.app.AlertDialog$Builder.show(AlertDialog.java:812)
com.scoreninja.adapter.ScoreNinjaAdapter.show(ScoreNinjaAdapter.java:136)
com.scoreninja.adapter.ScoreNinjaAdapter.show(ScoreNinjaAdapter.java:99)

I thought the main thread had prepare() called automatically, and if not, why would it work fine for me but not anyone else?


回答1:


I'm still not sure what was wrong, but for anyone who has the same problem, it works fine if you break up your game into a menu Activity and a game Activity. Have the game activity return a score to the menu activity on a game over, then call all the ScoreNinja stuff from the menu activity.




回答2:


I imagine you're creating the handler outside of the main thread. Create the handler in the onCreate of your activity or something similar, something that'll definitely be in the main thread, and then you can call handler.post(runnable) inside your other thread.



来源:https://stackoverflow.com/questions/2603846/scoreninja-causes-java-lang-runtimeexception-cant-create-handler-inside-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!