I have been having a lot of trouble because of an apparent bug in the latest java release, that has stopped me and many other people it seems, from referencing images. For example, the following code will not compile, even though this is the code given by many sites:
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
(or to a similar effect)
What I'm looking for is an alternate way to access an image from the file system, without using said "R.drawable.*".
2 possibilities:
Clean your project (Project>Clean). This will probably also fix your R.java file.
Check your imports! Possible that you've imported the R class. Had lots of trouble with that too.
If you're really looking for an alternative, look at Assets.
EDIT
If the above doesn't fix it, check your res folder (incl subfolders) for suspicious files, and delete them. like for example files that won't open, have no extension, or have errors in them.
Is that the error that you're getting when you compile the code that you've posted? I've never seen this error before. I would suggest doing a Project --> Clean and if that doesn't work delete the R file in the gen folder and then rebuild.
seen the same problem,make sure you have not imported the .R package,if so remove it from th list of imports,delete the R.java class ,clean then rebulid the project.
Related
I have spent all last night (until 3am) and this morning researching, testing, refactoring, and attempting to debug this issue. I have a simple Java game in Netbeans and while it runs perfectly perfect within the IDE in either run or debug mode, once exported into a jar file it refuses to load any resources corrrectly. There are many similar questions to this such as this one regarding loading an ImageIcon and despite great effort none of these solutions work for my project. I am not using ImageIcons, only simple BufferedImages and wav sound files. I recently refactored to combine my BufferedImageLoader and Sound classes into one Resource class, which I then moved into the same package as all my resources even though it worked perfectly well in a separate code package before in the IDE, although it works in its new location as well, strictly within the IDE.
I'm rather irritated and flustered from this issue. The truly infuriating thing is that this project used to work with resources after being exported into a jar, and now it seems to have stopped working with no changes. The only real programmatic difference between back when it worked and now is that I didn't have or use sound files back then, but this error isn't related to the sound files, as it catches an exception (and generates an error dialog) just from first trying to load the art assets.
I've tried every possible solution I've found in my research to no avail. Hopefully a fresh set of eyes can reveal the error of my ways.
The offending line of code is
return ImageIO.read(Resource.class.getResource("/res/" + imageFileName));
whereas imageFileName is the parameter with values passed from method calls such as
blockSheet = Resource.loadImage("art_assets/platform.png");
The location of the Resource class seemed to have no bearing on this working within Netbeans. My res folder is inside src, next to the com class package beginning.
It throws an IllegalArgumentException: input == null! exception. After some testing it seems that Resource.class.getResource("/res/" + imageFileName) returns a null value, which makes no sense at all. Again, this works perfectly perfect within the IDE. I can change the jar file into a zip and look inside to see that all the resources are exactly where they should be with the correct names and the correct extensions.
Here is a zip file of my entire project. Any help is immensely appreciated. Thank you.
EDIT:
Some of the things I've already tried:
getResourceAsStream() instead of getResource()
classLoader() between Resource.class and getResource()
this.getClass() instead of Resource.class from a non-static context
I think this should help:
How to get the path of a running JAR file?
CodeSource codeSource = YourMainClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
provided by Benny Neugebauer in the post.
Ok, I'm stumped here. I'm using Matlab version 2013b with a Java RTE of 1.7.0_11 and I'm trying to run a simple piece of code to see if Matlab is able to read the .jar file and nothing seems to be working.
Here is the Java code, which is compiled to a .jar named JavaOCT.jar, which is placed in the Matlab working directory:
package VTK;
public class vtkVolumeView{
public int Test(){
return 10;
}
}
That is it, no other dependencies, nothing fancy. In Matlab, I try:
javaaddpath('\JavaOCT.jar'); %<-Directory and name are 100% correct
import VTK.*; %<-Package name from above
methodsview VTK.vtkVolumeView; %<-Can't find the class, argh!
Matlab kicks back that it can't find the class.
Things I've done to try and solve the problem:
Reverted to the exact same JDK as the Matlab RTE
Tried an older 1.6 JDK
Done lots of stack overflow research to try and solve it 1 2 3 4
Tried used javaclasspath and pointing to the compiled class instead
Read the Matlab documentation 5
Using clear -java after the javaaddpath
Any help would be appreciated, it is driving me nuts!
Update: Daniel R suggested just javaaddpath('JavaOCT.jar') which doesn't work either.
Final update: It finally works! I wasn't building the .jar properly. In IntelliJ, click on the project and hit F4. This brings up the Project Structure, then go to Artifacts and click the green + button and add DirectoryContent and then point to the out\production. Once this is done, as mentioned by others, it should show up in Matlab as an expandable .jar.
I don't know which operating system you are using, but the ./ seems invalid.
Try javaaddpath('JavaOCT.jar'); or javaaddpath(fullfile(pwd,'JavaOCT.jar'));.
What does exist(fullfile(pwd,'JavaOCT.jar')) return?
Some things to try:
Add the class file. When using a package, you need to add the class file in at the host of the package. For example, if your code is here:
\\full\path\to\code\VTK\vtkVolumeView.class
Then use:
javaaddpath('\\full\path\to\code')
I'm still suspicious of your *.jar path. You should usually use absolute paths when adding jar files. Try adding the results of which('JavaOCT.jar')
How did you make your jar file? Does it contain the appropriate directory structure implied by your package declaration?
I've written a game that uses PNGs, and when I exported it, the images wouldn't work. I added getClass().getClassLoader().getResource() to everywhere I import an image, but the JAR won't even launch anymore. It used to open, but none of the images worked. Now it won't even open the JAR.
This is how I get the image for the muffin:
muffin=new ImageIcon(getClass().getClassLoader().getResource("muffin.png")).getImage();
Actual path for it is: C:\Users\My User Name\Dropbox\FinalProjectWithoutApplet\muffin.png
What should I do to solve this issue?
Thanks!
getClass().getClassLoader().getResource("muffin.png") is looking for the image muffin.png in the same directory where your class is.
I means that if your class's name is com.mycompany.game.MyClass the image is expected to be in com/mycompany/game/muffin.png. If this is not the location write absolute path that starts with /, for example /img/muffin.png.
And in future if program does not work start from examining the stack trace.
I found the issue. Turns out my cases mismatched. The IDE didn't care that one was called "muffin.png" in code vs. "Muffin.png". Another case I found was "DoubleJumpIcon.png" vs. "DoubleJumpIcon.PNG". Every single case matters it turns out.
I've been developing for android using processing but have come to a halt when I wanted to retrieve a list of files within a given directory. Below is a screenshot of the code I have been trying.
I have tried different variations of this (such as getbaseContext().getAssets();) and nothing seems to work. Whenever the code tries to execute the list() part it has an error and there is nothing in 'fileNames'.
Am I missing anything? Is this a problem with processing?
Thanks
EDIT: The direction I am trying to access is "assets/Levels/" which I can see from my project view in eclipse.
I experienced a similar issue because I used folder names with a trailing / character.
So for others experiencing this issue, you need to make sure the folder name you use are just the folder name with no trailing / (so "movies" instead of "movies/")
You do not need any additional permissions in order to run the code that you have. I just compiled it on my device and it works fine. You mention you are attempting to list the files in a given directory. What you are doing is listing the files in the Levels folder, which should be a sub folder of assets. What you will get back is an array of all file names in that folder.
If you are actually looking to get a list of files from a given directory, it has nothing to do with the assets folder and I suggest you check out a tutorial on Android external and internal storage. The information directly on the android page was very helpful to me.
http://developer.android.com/guide/topics/data/data-storage.html
If that's not what you are looking for though, I suggest rewording your question.
So I got it to work!
Turns out it was a problem with where I was placing the 'String[] fileNames;' code before trying to put data into it. The below code works fine:
Thanks for the help!
*Note : Individual folder should not empty.. It will return zero for folder with no files. *
void getFolderDetais(String folder_name)
{
// pass folder_name empty to get detail of root that means assets folder details
String[] filelistInSubfolder = assetManager.list(folder_name);
Log.v("Floder Details ----- >", " Length --- "
+ filelistInSubfolder.length);
}
I am having trouble with my R file, I was able to get it running again but then i seem to have another issue. Nothing seems to be generating after all the work I put into it. I think my problem is coming from the fact that my src cannot see the xml files and I don't know why. My find_bluetooth_devices is underlined in red squiggles and says it cannot be resolved or is not a field. However, I know for sure that I have created a xml file that included everything for it to work. I feel as though the xml file is not being seen by the src as it should be. Does anyone know what I can do in order for the src file to see my xml file?
setContentView(R.layout.find_bluetooth_devices);
Make sure your import statement is for the correct R file. You could have mistakenly imported android.R instead of your projects R file.
Here, read through this and the next one and see if you can solve the problem:
Also, make sure you dont have import android.R at the top.
Where did my R file go?
Try cleaning the project
Project>Clean
There are two things that can cause that:
There's a syntax error in one of your XML files, this causes the R file to not be generated.
The R file is not included in your class, make sure you import com.yourproject.R in the class.
Clean your project
the project will rebuild and R.java generated
Change the compiler the project will rebuild and R.java generated
Create New Project and copy all java file and resources into new project