所以我是 Android 新手,并且在理解线程/Android UI 更新等方面遇到了一些困难。我有一些为 AP 计算机科学期末项目制作的简单游戏引擎的代码,我一直在尝试将其制作成 Android 应用程序。来自java世界,我不习惯线程或担心计算发生的时间或位置,因此我在让我的游戏可玩时遇到了一些困难。在启动屏幕和主菜单之后,我将其设置为启动名为“Play”的活动。在此活动中,我发现我可以初始化我的游戏引擎对象(从一个类传递到另一个类),创建一个我创建的对象来创建 AsyncTask(下面链接的代码中的“GuiThreader”),但是一旦我投入“Play”中的一些代码可以执行更多操作(例如初始化按钮,或启动线程帮助器类),我收到“活动意外停止”错误,这意味着我做错了什么。我一直在查看很多 Android 示例代码,但它对我来说没有什么意义。所以我想有了这些背景,我更大的问题是如何让这段代码正常工作?更具体地说,我应该在哪里设置循环来检查游戏何时结束,以及如何在“主”线程之外更新按钮颜色以防止崩溃。
这是我的代码:
“Play”Activity(带有我正在测试的注释代码行):
http://pastebin.com/K5kFsMvG
“GuiThreader”用于使游戏计算成为 AsyncTask:http://pastebin.com/306eUYfq
“GUIdriver”用于调用更新按钮颜色的类: http://pastebin.com/RANZBH38
"ButtonColorUpdate " 保存按钮的值并更新其颜色:http://pastebin.com/qN2fw1RC
如果您需要其他任何内容,请发表评论,我会提出来。预先感谢您的任何帮助!
So I'm new to Android, and have been having some difficulties with understanding threading/Android UI updating and such. I have some code for a simple game engine I made for an AP computer science final project and I have been trying to make it into an Android app. Coming from the java world I'm not used to threading or worrying about how much time or where a calculation is taking place so I've been having some difficulties getting my game playable. After a splash screen and a main menu, I have it set up so an activity named "Play" starts. In this activity I have found that I can initialize my game engine object(which gets passed from class to class), create an object I created to make an AsyncTask("GuiThreader" in the code linked below) but as soon as I throw in some code in "Play" to do anything more than that (like initialize a button, or start the threader helper class) I get an "Activity has Stopped Unexpectedly" error meaning I'm doing something wrong. I've been looking at a lot of the Android example code but its making little sense to me. So I guess with all that background my bigger question is how can I get this code working? More specifically where should I have my loop that checks when the game is over, and how can I update my button colors outside of the "main" threader to keep it from crashing.
Here is my code:
"Play" Activity (with lines of code commented that I was testing with):
http://pastebin.com/K5kFsMvG
"GuiThreader" used to make the game calculations an AsyncTask: http://pastebin.com/306eUYfq
"GUIdriver" used to call a class that updates the button colors: http://pastebin.com/RANZBH38
"ButtonColorUpdate" saves a value for buttons and updates their colors: http://pastebin.com/qN2fw1RC
If you need anything else just comment and I'll put it up. Thanks in advance for any help!
发布评论
评论(1)
首先阅读本文。然后我建议查看 Replica Island 中的代码。通过研究这个项目的代码,您可以学到很多东西。如果您想从单独的线程与 GUI“东西”进行交互,则处理程序(请参阅 API)对于在两者之间发送消息非常有用。
Start by reading this. Then I'd recommend looking at the code from Replica Island. There's a ton you can learn from studying the code of this project. If you want to interact with GUI "stuff" from a separate thread, a Handler (see the API) can be useful for sending messages between the two.