简体   繁体   English

Android libGDX在启动时随机崩溃

[英]Android libGDX crashes randomly on startup

It seems the InputProcessor is trying to run before the entire program is finished initializing. 似乎InputProcessor试图在整个程序完成初始化之前运行。

How can I delay the InputProcessor ? 如何延迟InputProcessor

because I am randomly getting NullPointerExceptions when I first launch the program because the touchdown event is being triggered in the middle of the program loading (it only takes 2 seconds for the program to start up). 因为我第一次启动程序时会随机获得NullPointerExceptions ,因为在程序加载过程中触发了touchdown事件(程序启动仅需要2秒钟)。

I have 2 threads start up so I think I might need to pause in the middle of my create() method until the 2 threads have finished initializing their variables. 我有2个线程正在启​​动,因此我想我可能需要在create()方法的中间暂停,直到2个线程完成对它们的变量的初始化为止。

"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers." “寻求调试帮助的问题(“为什么此代码为什么不起作用?”)必须包括所需的行为,特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题说明的问题将无济于事给其他读者。”

desired behavior: my create thread to wait for my other 2 threads to finish is how I am thinking this problem will be resolved. 所需的行为:我的创建线程等待其他2个线程完成,这是我认为将解决此问题的方式。

Here is a little diagram of my code 这是我的代码的小图

//create thread 1
//create thread 2
//initialize variables
//initialize InputProcessor
//infinite while loop

//helper thread 1
//initialize some variables
//infinite while loop

//helper thread 2
//initialize some variables
//infinite while loop

the problem is the create thread has an input that seems to take input even before the program is finished launching. 问题是创建线程的输入似乎在程序启动完成之前就接受了输入。 This glitch only happens within the first second of launching occasionally. 该故障仅在发射的第一秒内发生。

why not use a shared AtomicBoolean between the thread to signal that InputProcessor is safe to initialize. 为什么不在线程之间使用共享的AtomicBoolean来表示InputProcessor可以安全初始化。

Or you could use Thread.join to wait for the thread completion in the main thread. 或者,您可以使用Thread.join等待主线程中的线程完成。

It would be difficult to give further answer without seeing some minimal code. 如果不看一些最少的代码,将很难给出进一步的答案。

Update : 更新

According to your diagram sequence, The main thread hold reference to thread_1 and thread_2 , each of these thread instance could have an AtomicBoolean value, the main thread could check both in a loop before continuing on the initialization. 根据您的图序列, main thread持有对thread_1thread_2引用,这些线程实例中的每个线程实例都可以具有AtomicBoolean值, main thread可以在继续初始化之前在循环中检查这两个值。

Or you could also use Condition variable ( android specific or java ), to blick the main thread until the child thread init is done. 或者,您也可以使用Condition variable特定androidjava )来隐藏main thread直到完成子线程初始化为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM