简体   繁体   中英

Android libGDX crashes randomly on startup

It seems the InputProcessor is trying to run before the entire program is finished initializing.

How can I delay the 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).

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.

"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.

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.

Or you could use Thread.join to wait for the thread completion in the main thread.

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.

Or you could also use Condition variable ( android specific or java ), to blick the main thread until the child thread init is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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