R file cannot see my src file - java

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

Related

Declaring packages in eclipse

I'm new to java and eclipse and was wondering if someone could help me.
In my comp class we have a lab each week so in eclipse I created a folder called "compClass" in this file is the "src" folder and in that folder are 3 packages. lab1, lab2, lab3, one for each week we have an assignment. We're currently on assignment 3 so I'm using "lab3" package. In this package I have 2 files, "Test.java" and "MyInteger.java". These files were given to the students and we are supposed to modify them, however that's not my issue.
My issue is on those files I keep getting a compiler error that says: The declared package "" does not match the expected package "lab3" I'm not sure why this is happening as I didn't import anything. I created new java files and copy pasted the code in from a text file. I even tried typing it out by hand and got the same error. I tried dragging and dropping the "Test" and "MyInteger" files (as java files not text files) from a folder on my desktop to the "lab3" package and got the same error. How can I fix this?
P.S. On this post: https://stackoverflow.com/questions/7628686/eclipse-the-declared-package-does-not-match-the-expected-package/13444301#:~:text=java%20files%20to%20%22package%20path,All%20should%20be%20ok.&text=If%20you%20have%20imported%20an,the%20error%20till%20you%20restart.
The top answer said to find the src/prefix1 directory and right click on it. Then build path and use as source folder. If that is the answer then could someone please help me find the "src" directory I don't even really know what that is or where to find it.
Thank you!
Every Java project has a project root which can be configured using your IDE. In your case, the root is src/. If your code is inside a subfolder in the root, this folder represents the package your code belongs to, so in this case it's a package of lab3. If you want to not include any package declarations, then either place the code directly inside src or set lab3 as the root.

Can't resovle a class in jar that already added in my project

As you can see from the pic, StdOut is a error for can't be resolved. But when I click it with holding Ctrl, I can still open the StdOut.class(Also in pic,you can see it) And StdOut class is in stdlib.jar which is in Referenced Libraries in left of pic. I just want to use StdOut.class. What should I DO?
Try adding the jar as an external jar
BuildPath>>Configure BuildPath >>Libraries>> Add External Jar
Also try importing the full path of StdOut before using it.
I found a stupid solution: find the StdOut.class and copy the content from it, make a new package in my project and paste it. Although it's stupid, but this time StdOut.class can be resolved (the Eclipse can resolve my knew package).

Unknown XML Block error in Eclipse

I am trying to build my Android project in Eclipse and this error is popping up in my console:
W/ResourceType( 265): Unknown XML block: header type 0 in node at 413
This is causing my R.java to not generate. I have absolutely no idea what this means. It's not even telling me which file is causing the error. This is quite a large project with many XML files and I've looked through every single one and they all seem to be okay. Can anyone shed some light on this?
Any help would be greatly appreciated.
R.java includes all Ids, layouts.. If there is any problem like formating error in id name or any other error of layout. then R.java file will not be generated & your java class file will show you error. so first check your xml file
There might be the possibility that you have put your xml file in the wrong folder under res dir. Like if you are trying to access <wallpaper> tag in Android xml file you must put it in res/xml directory otherwise it will show error.
You can try following options::
close other projects.
clean your project.
check in the source files(java classes), if there import android.R.* is present , remove that.
check in your res folder, if it is showing error in any folders like drawable, layout, values.
5.correct if you get errors.
R.java is not coming because it is not getting id of any/more resource/resources.
this will solve your problem. thanks
Your error may come from incorrect line ending. You should check this Link. Hope this help.

Opening a xml file from eclipse and from a .jar file in java

Yesterday, I had a problem because I couldn't manage to open a xml file (it owuld give me a FileNotFoundException) located in the ressources folder of my .jar file, which I managed to open on eclipse using the following lines of code. You can see my old problem here. This was my code with the problem :
File xmlFile = new File("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(xmlFile);
Someone told me it that one way was to use getClassLoader().getRessourceAsStream method to open a xml file in a .jar file that was exported
InputStream i= this.getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(i);
Unfortunately, that solution only works when I export my project into a .jar file, so if I want to go back debugging my program, I have to take the old code that would only works on eclipse.
My question is: is there any better way to do this without having to change my code if I want to export it or if I want to debug it?
Thank you
edit :
Thank you all, it works perfectly fine now
my problem was that I put my ressources folder like that :
+project
+src
+ressources
+emitter.xml
InputStream i= this.getClass().getClassLoader().getResourceAsStream("/ressources/emitter.xml");
The above should work in both cases (Note is is /resources/.... This is assuming say your directory structure is below:
MyProject
+src
+ressources
emitter.xml
Place the file alongside your source files, then you can use the getResourceAsStream() method in both cases. Don't forget to update the path (which should be the package name of your class, but with slashes instead of dots).
My question is: is there any better way to do this without having to
change my code if I want to export it or if I want to debug it?
Yes, use Maven. Maven will handle that and it hooks into Eclipse beautifully (NetBeans too!) What you do is place the resource in src/main/resources and then you can have Eclipse run the test goal of the Maven project or you can just run mvn test from the command line. Another advantage of using Maven here is that you can also have src/test/resources/emitter.xml which overrides the one in src/main with environment-specific test instructions and it won't affect your deployment.
InputStream i= getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
or
InputStream i= getClass().getResourceAsStream("/ressources/emitter.xml");
(note the absolute positioning)
both work when the class is in the same jar, on the same class path.
In the jar the names must be case sensitive, but as the jar already works. Ensure that the ressources directory is on the class path too, or copied to the target directory.
As "ressources" is probably configured yourself (not named "resources" as in English), you probably need to add it to the build somehow.

Looking for alternative to R.drawable.*

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.

Categories

Resources