why can not I set drawable to find the package name? - java

I can not understand what is the error that I have, what I did wrong? I have followed the various tutorials on the internet for how to set up an image from drawable, because it does not work?
Drawable drawable = mContext.getResources().getDrawable(
mContext.getResources().getIdentifier(
sampleItem.get(position).getImage(), "drawable",
mContext.getPackageName()));
image.setImageDrawable(drawable);
Logcat:
W/ResourceType(29255): No package identifier when getting value for resource number 0x00000000
D/AndroidRuntime(29255): Shutting down VM
W/dalvikvm(29255): threadid=1: thread exiting with uncaught exception (group=0x4331c1a0)
E/AndroidRuntime(29255): FATAL EXCEPTION: main
E/AndroidRuntime(29255): Process: com.treffedin.navigationdrawerdemo, PID: 29255
E/AndroidRuntime(29255): android.content.res.Resources$NotFoundException: Resource ID

Try cleaning and building the project.
Android is searching the image with id resource 0x00000001, which is not correct.
If that doesn't works try deleting the "gen" folder and building again.
Good luck :)

I solved,
changed
sampleItem.setImage ("image_0.png");
to
sampleItem.setImage ("image_0");
beginner's mistake.

Related

Couldn't load file: assets/libgdx128.png

I'm building a game in LibGDX and have began adding visual aspects of the game such as the logo. Considering that I've been using the default libgdxXXX.png files as placeholders for my logo, and that I'm replacing them with my logo, I didn't feel it had any more purpose in my program. I decide to delete it; but it decide to not compile and come up with this error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: assets/libgdx128.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:148)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:196)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:142)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: assets/libgdx128.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:145)
... 3 more
Somehow, when I return the files from the Trash (I use Linux), it works again! I have checked my code and found nothing I wrote referred to those files. When I deleted the files 1 by 1, the error changes from libgdx128.png to libgdx64.png to libgdx32.png to libgdx16.png respectively. I have changed all the references from libgdx128.png to my logo.png in my code; yet when I try to delete the libgdx logo and run my program, it fails to compile! Any ideas?
I found out my problem. I forgot all about this code at DesktopLauncher.java:
for (int size : new int[] { 128, 64, 32, 16 }) {
configuration.addIcon("assets/libgdx" + size + ".png", FileType.Internal);
}
I did not know it was referencing to the files I have deleted. The configuration.addIcon(); was the code setting the program icon - similar to how a favicon works. I will be adding my own icon now that I know what the code does.
Hope this helps anyone else confused about the error.

BodyEditorLoader - error reading file

So I have recently started learning libgdx in Android Studio. I am using some extensions like box2d. To create some nice borders around the object, I am using Physics Body Editor. So, as there were many errors in my own code, i have just copied one from here: Code Sample. But still, I keep getting an error on the following line:
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("tankA.json"));
The file tankA.json is in my assets folder.
Here is my error:
02-12 08:27:52.216 13720-13734/com.tynibattles04.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 6581
Process: com.tynibattles04.game.android, PID: 13720
com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: tankA.json (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:605)
at com.badlogic.gdx.backends.android.AndroidFileHandle.length(AndroidFileHandle.java:220)
at com.badlogic.gdx.files.FileHandle.estimateLength(FileHandle.java:233)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:198)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:191)
at aurelienribon.bodyeditor.BodyEditorLoader.<init>(BodyEditorLoader.java:41)
at com.tynibattles04.game.TinyBattles.createBottle(TinyBattles.java:127)
at com.tynibattles04.game.TinyBattles.create(TinyBattles.java:74)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1512)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: java.io.FileNotFoundException: tnkA.json
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:355)
at android.content.res.AssetManager.open(AssetManager.java:329)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
Do you know what could i do with all that?
Thanks in advance!
I have accidentally misspelled tankA.json in the error code - thanks to Angel Angel. But I still keep getting the same error. Maybe that's be cause something wrong this the BodyEditorLoader?
Check the file name and the name you use to load, then you say:
BodyEditorLoader loader = new BodyEditorLoader(
Gdx.files.internal("tankA.json"));
But in stack says:
com.badlogic.gdx.utils.GdxRuntimeException:
Error reading file:
tnkA.json (Internal)
maybe this is an error in the file name: tnkA.json
note: maybe in your code use, tnkA.json and not tankA.json as you think.
hope it helps.

Baksmali error for android odex file

I downloaded core.odex and framework.odex from my Samsung Galaxy Player, in order to make some changes for the hidden apis, following this webpage: https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/
Also, I got baksmali and smali from here: http://forum.xda-developers.com/showthread.php?t=1650243
Now, when I run the command: java -jar baksmali-1.3.3.jar -c :core.odex -x framework.odex, the following error appears:
UNEXPECTED TOP-LEVEL EXCEPTION:
org.jf.dexlib.Util.ExceptionWithContext: regCount does not match the number of arguments of the method
at org.jf.dexlib.Util.ExceptionWithContext.withContext(ExceptionWithContext.java:54)
at org.jf.dexlib.Code.InstructionIterator.IterateInstructions(InstructionIterator.java:92)
at org.jf.dexlib.CodeItem.readItem(CodeItem.java:154)
at org.jf.dexlib.Item.readFrom(Item.java:76)
at org.jf.dexlib.OffsettedSection.readItems(OffsettedSection.java:48)
at org.jf.dexlib.Section.readFrom(Section.java:143)
at org.jf.dexlib.DexFile.<init>(DexFile.java:431)
at org.jf.baksmali.main.main(main.java:265)
Caused by: java.lang.RuntimeException: regCount does not match the number of arguments of the method
at org.jf.dexlib.Code.Format.Instruction3rc.checkItem(Instruction3rc.java:129)
at org.jf.dexlib.Code.Format.Instruction3rc.<init>(Instruction3rc.java:79)
at org.jf.dexlib.Code.Format.Instruction3rc.<init>(Instruction3rc.java:44)
at org.jf.dexlib.Code.Format.Instruction3rc$Factory.makeInstruction(Instruction3rc.java:145)
at org.jf.dexlib.Code.InstructionIterator.IterateInstructions(InstructionIterator.java:84)
... 6 more
Error occured at code address 0
code_item #0x159a90
What am I missing or doing wrong?
Appreciate in advance any help you can provide.

java.lang.RuntimeException: Resource not found?

I can't understand why I'm getting this error: java.lang.RuntimeException: Resource not found I'm trying to make a simple 2d game using Slick and LWJGL libraries, I followed this guide http://www.youtube.com/playlist?list=PLaNw_AbDFccGkU5gnFYquQ0PNQPmmD-Q7 and I managed to make even some more by myself.
The thing is that I am receiving this error even though the image does exist in the specified location. The game runs completely fine and suddenly quits with the error I already mentioned.
The error:
Wed Nov 27 14:43:46 PST 2013 ERROR:Resource not found:
/home/tomtam/workspace/Game/gfx/world/object/blockgreen.png
java.lang.RuntimeException: Resource not found:
/home/tomtam/workspace/Game/gfx/world/object/blockgreen.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:273)
at org.newdawn.slick.Image.<init>(Image.java:270)
at org.newdawn.slick.Image.<init>(Image.java:244)
at org.newdawn.slick.Image.<init>(Image.java:232)
at org.newdawn.slick.Image.<init>(Image.java:198)
at tomtam.game.object.BlockGreen.render(BlockGreen.java:18)
at tomtam.game.main.World.render(World.java:447)
at tomtam.game.state.PlayState.render(PlayState.java:76)
at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:207)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:703)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
at tomtam.game.main.Main.main(Main.java:36)
Wed Nov 27 14:43:46 PST 2013 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:706)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
at tomtam.game.main.Main.main(Main.java:36)
So the error points to this part of the code (image):
public void render(GameContainer gc, Graphics g) throws SlickException
{
super.render(gc, g);
image = new Image("/home/tomtam/workspace/Game/gfx/world/object/blockgreen.png");
}
I am not a skilled programmer, but I guess that this error is because the image is getting rendered nonstop, even if it's location or other information haven't changed. That may produce some lag spikes, right..? So, I tried to change it to:
try
{
image = new Image("/home/tomtam/workspace/Game/gfx/world/object/blockgreen.png");
}
catch (RuntimeException npe)
{
}
I know that it's a bad thing to do it like this, however, this way I am not receiving any errors and everything works fine for some time. After some time some images starts blinking and the more I wait, the less time they are showing up until finally disappearing.
The code is kinda long, but I can post it, just ask. Any help will be appreciated.
Usually a "java.lang.RuntimeException: Resource not found" occurs when your resources (images now) are not in your CLASSPATH and generally it is a classpath issue.
This may also help you.

Java game not working

I found this java source code for a brick breaker game online. I downloaded it and tried to run it but it gives me a NullPointerException. What is giving this exception and how do I fix it? Thanks.
http://zetcode.com/tutorials/javagamestutorial/breakout/
Here is the stacktrace:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at Ball.<init>(Ball.java:16)
at Board.gameInit(Board.java:48)
at Board.addNotify(Board.java:43)
at java.awt.Container.addNotify(Container.java:2578)
at javax.swing.JComponent.addNotify(JComponent.java:4685)
at java.awt.Container.addNotify(Container.java:2578)
at javax.swing.JComponent.addNotify(JComponent.java:4685)
at java.awt.Container.addNotify(Container.java:2578)
at javax.swing.JComponent.addNotify(JComponent.java:4685)
at javax.swing.JRootPane.addNotify(JRootPane.java:739)
at java.awt.Container.addNotify(Container.java:2578)
at java.awt.Window.addNotify(Window.java:663)
at java.awt.Frame.addNotify(Frame.java:470)
at java.awt.Window.show(Window.java:859)
at java.awt.Component.show(Component.java:1584)
at java.awt.Component.setVisible(Component.java:1536)
at java.awt.Window.setVisible(Window.java:842)
at Breakout.<init>(Breakout.java:14)
at Breakout.main(Breakout.java:18)
Exception in thread "Timer-0" java.lang.NullPointerException
at Board$ScheduleTask.run(Board.java:110)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Apparently this code is supposed to work, but it isn't.
You probably didn't download the images as well, or at least didn't put them in the right folder. Make sure you put the images here:
Browse to Ball.java, go two folder levels up, create there a folder called images and put there your images.

Categories

Resources