Why does my game loop give me a blank screen?

AI Thread Summary
The user is attempting to implement a game loop to alternate text in a TextView every two seconds but is encountering a blank screen instead of the expected output. The initial TextView content, "Hello World!", is not displayed. The provided code continuously updates the TextView without yielding control back to the UI thread, leading to resource exhaustion and preventing the UI from rendering. The solution involves incorporating a sleep mechanism within the loop to allow the UI thread to refresh and display the text correctly. Continuous execution without breaks is not suitable for UI updates in Android, as it blocks the main thread.
Darkmisc
Messages
222
Reaction score
31
TL;DR Summary
I've used a game loop to try make a textView alternate between two different strings every two seconds. I just get a blank screen.
Hi everyone

I've used a game loop to try make a textView alternate between two different strings every two seconds. When I run it, I just get a blank screen. The textView was originally set to show "Hello World!" but not even that shows.

Does anyone know what's gone wrong?

Thanks

[CODE lang="java" title="GameLoop"]public void GameLoop() {
double timeChange = 0;
double time1 = System.nanoTime();
double time2;

while (true) {
time2 = System.nanoTime();
timeChange += (time2 - time1) / 1000000000;
time1 = time2;
textView.setText("fadsjfsdjfklsjfklsadjf");
if (timeChange > 2) {
textView.setText("2");
timeChange = 0;
}
}
}[/CODE]
Logcat says:

[CODE lang="java" title="logcat"] E Not going to handle 'com.xxmassdeveloper.gamelooptest'!
2023-03-01 09:45:53.278 3598-3598 SDAgentPac...teReceiver pid-3598 E Not going to handle 'com.xxmassdeveloper.gamelooptest'!
---------------------------- PROCESS STARTED (25862) for package com.xxmassdeveloper.gamelooptest ----------------------------
2023-03-01 09:45:55.291 25862-25862 AppCompatDelegate com.xxmassdeveloper.gamelooptest D Checking for metadata for AppLocalesMetadataHolderService : Service not found
2023-03-01 09:45:55.388 25862-25862 PhoneWindow com.xxmassdeveloper.gamelooptest D forceLight changed to true [] from com.android.internal.policy.PhoneWindow.updateForceLightNavigationBar:4238 com.android.internal.policy.DecorView.updateColorViews:1510 com.android.internal.policy.PhoneWindow.dispatchWindowAttributesChanged:3216 android.view.Window.setFlags:1148 com.android.internal.policy.PhoneWindow.generateLayout:2444
2023-03-01 09:45:55.390 25862-25862 MultiWindowDecorSupport com.xxmassdeveloper.gamelooptest I updateCaptionType >> DecorView@2059f86[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2023-03-01 09:45:55.391 25862-25862 MultiWindowDecorSupport com.xxmassdeveloper.gamelooptest D setCaptionType = 0, DecorView = DecorView@2059f86[]
2023-03-01 09:45:55.626 25862-25862 er.gamelooptes com.xxmassdeveloper.gamelooptest W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2023-03-01 09:45:55.628 25862-25862 er.gamelooptes com.xxmassdeveloper.gamelooptest W Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
---------------------------- PROCESS ENDED (25862) for package com.xxmassdeveloper.gamelooptest ----------------------------
2023-03-01 09:47:39.554 3598-3598 SDAgentPac...teReceiver pid-3598 E Not going to handle 'com.xxmassdeveloper.gamelooptest'!
2023-03-01 09:47:39.786 3598-3598 SDAgentPac...teReceiver pid-3598 E Not going to handle 'com.xxmassdeveloper.gamelooptest'!
---------------------------- PROCESS STARTED (26539) for package com.xxmassdeveloper.gamelooptest ----------------------------
2023-03-01 09:47:41.959 26539-26539 AppCompatDelegate com.xxmassdeveloper.gamelooptest D Checking for metadata for AppLocalesMetadataHolderService : Service not found
2023-03-01 09:47:42.042 26539-26539 PhoneWindow com.xxmassdeveloper.gamelooptest D forceLight changed to true [] from com.android.internal.policy.PhoneWindow.updateForceLightNavigationBar:4238 com.android.internal.policy.DecorView.updateColorViews:1510 com.android.internal.policy.PhoneWindow.dispatchWindowAttributesChanged:3216 android.view.Window.setFlags:1148 com.android.internal.policy.PhoneWindow.generateLayout:2444
2023-03-01 09:47:42.044 26539-26539 MultiWindowDecorSupport com.xxmassdeveloper.gamelooptest I updateCaptionType >> DecorView@2059f86[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2023-03-01 09:47:42.044 26539-26539 MultiWindowDecorSupport com.xxmassdeveloper.gamelooptest D setCaptionType = 0, DecorView = DecorView@2059f86[]
2023-03-01 09:47:42.235 26539-26539 er.gamelooptes com.xxmassdeveloper.gamelooptest W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2023-03-01 09:47:42.237 26539-26539 er.gamelooptes com.xxmassdeveloper.gamelooptest W Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
---------------------------- PROCESS ENDED (26539) for package com.xxmassdeveloper.gamelooptest ----------------------------
[/CODE]
 
Last edited by a moderator:
Technology news on Phys.org
Darkmisc said:
Does anyone know what's gone wrong?
You can't just keep running forever - even game loops have to sleep sometimes!
 
  • Haha
  • Like
Likes berkeman and Darkmisc
UI thread will no resource to draw if you setText endless without quit the current method which is the same thread. And UI thread is not for holding your code for long time without release periodically.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
2K
Back
Top