I am trying to get LibGDX to take a screen shot of the devices screen and then load that screenshot into a Sprite to render later. However when I try it the app crashes with a NullPointerException. I have tried the following links: taking screenshot in libgdx, and using ScreenUtils to save screenshot as image in libgdx. When I try to create the Texture for the Sprite then it crashes with NullPointerException.
I have also tried the ScreenUtils.getFrameBufferTexture(); method, and the ScreenUtils.getFrameBufferPixmap() method but always they crash with a NullPointerException. The line they crash on changes depending upon the way I have chosen to get the screen shot, and I have confirmed that indeed the Pixmap or TextureRegion is created when the screenshot is taken. It is when I try to either load it into a Texture or the Sprite that it crashes with a NullPointerException.
PLEASE DO NOT mark this as a duplicate question as I am not trying to save the screenshot into a PNG. I am trying to use it directly in a Sprite and it is crashing.
Code and stack trace for creating a TextureRegion:
// create the screen shot
UT.screenShot = ScreenUtils.getFrameBufferTexture(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Use screenshot
screenShot = new Sprite(UT.screenShot);
I get this stack trace:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112)
at com.badlogic.gdx.graphics.g2d.Sprite.<init>(Sprite.java:83)
at *** MY CODE *** CompleteScreen.java:83
at *** MY CODE *** GameEngine.java:82
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
This is only one of the ways I have used to create the screenshot. But all the ways use the ScreenUtils class to get the screenshot. All these ways do indeed create a screenshot. However all these ways fail when trying to create a Sprite or Texture.
Code and stack trace for creating a Pixmap:
// create the screen shot
UT.screenShot = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Use screenshot
Texture texture = new Texture(UT.screenShot);
screenShot = new Sprite(texture);
I get this stack trace:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.glutils.PixmapTextureData.<init>(PixmapTextureData.java:37)
at com.badlogic.gdx.graphics.glutils.PixmapTextureData.<init>(PixmapTextureData.java:32)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104)
at *** MY CODE *** CompleteScreen.java:84
at *** MY CODE *** GameEngine.java:82
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
So I have solved this problem only to create another problem. Oh the joys of programming! ;-) Anyway I will ask about my second problem in another question. The answer to this question is that I was getting rid of the Pixmap or TextureRegion too soon.
What kind of code do you get at the end of programming for 12 hours straight? Bone headed code!
Oh well lesson learned, only program for 11 hours. ;-)
Related
In Google Play Console on the dashboard of your application there
is a graph with Crashes & ANRs.
What is the best practice to solve
crashes to provide the best service to your users? My application is
written in Python in Kivy framework.
Navigate to Quality -> Android Vitals -> Crashes and ANRs.
This lists each event and on the right you will see a "->" arrow labelled "View Details".
Click this, scroll down past the graph and you'll see the Stack traces.
This is what you need to look at to fix your code.
The top line will be the error, then it'll show the line of code where the error occurred, for example:
[code]
java.lang.NullPointerException:
at com.test.MyApp.doSomethingCool (MyClass.java:123)
at com.test.MyApp.somethingElse (AnotherClass.java:321)
[/code]
Here you have some null value causing an issue in the "doSomethingCool" function of your app and that code should be on line 123 of "MyClass.java".
The trace lines after that show what called that function that failed, so above, this is showing that "doSomethingCool" was called from function "somethingElse" on line 321 and that code is in "AnotherClass.java".
This example is for Java.
--Updated--
If you are coding in Python, then the stack trace may not show you where in your own code the problem lies. Instead your best bet is to search on Google for the top line error and hope that the answer is online.
Separately from stack trace analysis I would highly recommend that you place your project into source control with GIT or SVN, specifically so that you can see what changes might have caused an error.
Update your code in the repository only when it's working and then you will always have a safe saved version that is known as working. You can then easily compare your current code and see what changes might have caused an error. This is especially useful for Android, which regularly updates versions of shared libraries.
I'm working on a Unity project for Hololens, that uses the camera to capture pictures, send them to a photo recognition API and displays the result. The project works perfectly fine in unity, but not on the emulator/Hololens.
Unfortunately, I wrote a lot of code at once, so i don't know at what point this problem started. The problem show's up after building the project and running it on the Hololens/emulator in Debug mode. On the Hololens, I see the starting window (the one you see after you open any application). After i place it, i see End showing splash screen. on the Output window in Visual Studio, and it just doesn't go any further (but doesn't freeze either, just does nothing).
I don't know where it's coming from, since no exceptions are thrown, but i suspect the camera is the cause. Earlier, i had to comment this line of code:
transform.position = Camera.main.ScreenToWorldPoint(new Vector3((CameraManager.Resolution.width * .5f), (CameraManager.Resolution.height * .5f), 10));
because the function ScreenToWorldPoint was throwing the following exception:
Screen position out of view frustum (screen pos 0.000000, 0.000000, 10.000000) (Camera rect 0 0 0 0)
As you see it says that the Camera rect's size is 0. I even tried directly logging the camera's dimensions to make sure (Debug.Log(Camera.main.pixelWidth + ", " + Camera.main.pixelHeight)), and sure enough, they were (0, 0) on the Hololens/emulator.
I made sure that webcam is supported, and that my camera settings are all set, but that didn't help either.
So i'm not sure if that's the cause of the problem or simply a symptom. And I can't start anywhere since neither the Output nor the Error window show anything wrong. Any help or suggestions would be greatly appreciated.
Thanks for reading!
Edit: Here's the entire output log from beginning to end.
Edit2: I don't know if this is significant, but if I paused execution (in Visual Studio), it always seem to be at Build/ProjectName/App.css => Line 78:
[MTAThread]
static void Main(string[] args)
{
var app = new App();
CoreApplication.Run(app); //<===== Here
}
You might want to check any of your Start() methods. You might have some code that is CPU intensive. Even if it runs smoothly in Unity, doesn't mean it will run easily on HoloLens since their CPU is not powerful.
Also, to avoid any Camera problems, make sur to use the Camera prefab from this
repository :
https://github.com/Microsoft/MixedRealityToolkit-Unity
Those are just some thoughts, hope it helps!
Turns out i didn't enable "Virtual Reality Supported" under Other settings in PlayerSettings. It's really dumb, but i hope this helps someone.
Getting NullPointerException when try to load files for Particle Effect.
particleEffect = new ParticleEffect();
particleEffect.load(Gdx.files.internal("hdpi/particles/progressbar"), Gdx.files.internal("hdpi/particles"));
And here is stack trace:
Exception in thread "Timer-0" java.lang.NullPointerException
at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1372)
at com.badlogic.gdx.backends.lwjgl.LwjglGL10.glGenTextures(LwjglGL10.java:240)
at com.badlogic.gdx.graphics.Texture.createGLHandle(Texture.java:166)
at com.badlogic.gdx.graphics.Texture.create(Texture.java:158)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:126)
at com.badlogic.gdx.graphics.g2d.ParticleEffect.loadTexture(ParticleEffect.java:198)
at com.badlogic.gdx.graphics.g2d.ParticleEffect.loadEmitterImages(ParticleEffect.java:193)
at com.badlogic.gdx.graphics.g2d.ParticleEffect.load(ParticleEffect.java:141)
at com.yyy.xxx.Screens.GameScreen.<init>(GameScreen.java:38)
at com.yyy.xxx.MyScreen$3.getScreenInstance(MyScreen.java:27)
at com.yyy.xxx.ScreenManager.show(ScreenManager.java:33)
at com.yyy.xxx.ScreenChangeTask.run(ScreenChangeTask.java:15)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Any suggestions why?
UPDATE:
I tryed to run on android and it works, BUT I get something like this:
but if I turn off phone screen and turn on it back, than I see this, how it should be:
I think this Update will reply on first comment.
1) issue with desktop is not resolved
2) new issue with bad particles images on app start...
I had this code
particleEffect = new ParticleEffect();
particleEffect.load(Gdx.files.
internal("hdpi/particles/progressbar.p"),
Gdx.files.internal("hdpi/particles/"));
in constructor of public class GameScreen implements Screen. I moved this code into that place, where all assets are loaded ONCE in my app. This resolved both issues. Now it is working on Desktop, and there is no problems with bad rendering of particles.
When I start up my application I get the error detailed below. If I continue the application everything seems to be working fine. The error is raised between the calls to applicationWillFinishLaunching and applicationDidFinishLaunching.
I suspect the eror is getting generated when the xib is being loaded, however I can see nothing wrong with the xib and how it is connected up. Can anyone give me some tips on how I can trace back to find the source of the error?
Thanks,
2012-08-21 11:31:55.293 ConjugationViewer[32508:707] *** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1138.47/AppKit.subproj/NSCell.m:1564
2012-08-21 11:31:55.303 ConjugationViewer[32508:707] Ignoring exception raised in __-[NSPersistentUIManager restoreAllPersistentStateWithTalagentWindows:registeringAsReadyWhenDone:completionHandler:]_block_invoke_3: Invalid parameter not satisfying: aString != nil
I had same problem.
In my case I had set
[NSTextField setStringValue:nil] By mistake.
You can trace the problem in your code using crash stack log.
The log shows function call as stack start from bottom. You can search the last call of your function after which cocoa functions get called. You can say problem present at the same function.
Background
I'm using UKCrashReporter in my app.
I've installed my own Uncaught
Exception Handler.
I'm setting up the
managedObjectContext of the object
activeItemController in
applicationDidFinishLaunching (1)
The Problem
If the managedObjectContext method throws an exception, the crash reporter dialog box only flashes up before the app crashes and so the user never gets to report the crash.
I want my app to continue only after the crash has been reported, not whilst the window is showing.
What I've tried
If UKCrashReporterCheckForCrash()
were an objective C method, I assume
I could call
performSelectorOnMainThread:waitUntilDone:YES
but it's not.
I've looked at some other Stack
Overflow questions about using
Conditional Locks to pause apps,
but I can't understand how I'd use it
for a C function.
How would I go about doing this in a nice way? Do people have any advice
for me? Any responses would be much
appreciated.
The Code
// In app delegate
-(void)applicationWillFinishLaunching:(NSNotification *)aNotification {
UKCrashReporterCheckForCrash(); // A C function which then creates a window if
// it detects a crash has happened.
}
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[activeItemController setMoContextDisk:[self managedObjectContext]];
[activeItemController setMoContextMemory:[self managedObjectContextMemory]];
}
Update 1
I've been asked for more details on what I'm trying to do, so here goes.
The bug that triggered this thinking was an exception when merging managedObjectModels. My app got caught in a loop printing "Uncaught exception" to the console every few milliseconds.
And when I installed the uncaught exception handler before this exception happened, I'd get the described behaviour - my app would fire up, display the crash report dialog briefly, then continue to load and crash again.
Summary - I want to be able to handle errors that happen on startup.
(1) I'm not using bindings to do this, as I thought bindings would make testing the class more problematic.
I think your problem is with thinking of it as "pausing" your app. Think of it more as a different initial UI state. Your attempts to block the run loop will prevent any interactive window from ... well, being interactive. :-)
Your best bet is to show your main UI (and connect data sources, etc) only if the "am I prompting the user to submit a crash report" method says "no, go ahead and start normally". Otherwise, show your window and, when the user sends or declines to send the report, close the window and ask your app controller to continue the normal startup.
I looked at UKCrashReporterCheckForCrash() and it doesn't appear to create a window of any kind. It merely submits the crash. Could you describe what you're doing with more detail?