ScoreNinja 导致 java.lang.RuntimeException: 无法在未调用 Looper.prepare() 的线程内创建处理程序
我正在尝试将 ScoreNinja(全球高分系统)添加到我的 Android 游戏中,当我将其加载到手机上时它运行良好,但是当我将其发布到野外时,我收到崩溃报告说
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
:调用堆栈:
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)
我认为主线程自动调用了prepare(),如果没有,为什么它对我来说可以正常工作,但对其他人却不能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我仍然不确定出了什么问题,但对于任何有同样问题的人来说,如果您将游戏分解为菜单活动和游戏活动,则效果很好。让游戏活动在游戏结束时将分数返回到菜单活动,然后从菜单活动中调用所有 ScoreNinja 内容。
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.
我想您正在主线程之外创建处理程序。在 Activity 的
onCreate
中创建处理程序或类似的内容,肯定会在主线程中,然后您可以在其中调用handler.post(runnable)
你的另一个线程。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 callhandler.post(runnable)
inside your other thread.