raw folder cannot be resolved - android - java

I am using AndroidStudio, which is based on intelliJ Idea. I know that this issue was often presented here, but I created inside res folder some raw folder and copy-pasted some mp3 file jam.mp3. But I can't make my raw folder (inside my res one) resolved by my IDE and I tried numbers of tricks suggested in SO.
I have this line of code
MediaPlayer mp = MediaPlayer.create(this, R.raw.jam);
and raw cannot be resolved, how can I fix it ? (Btw, I can't find in my IDE an equivalent of the eclipse's clean)

In IntelliJ IDEA, you have a context menu option "Force generate R.java" when you stand on such a resource file.
Your song name should start with Capital Letter , Should not contain special characters like . _ etc..
If this not solves your problem then try restarting your IDE.

Related

Weird compile error in Eclipse and Netbeans

I am getting an error : Syntax error on tokens, delete these tokens, on valid Java code
My code, copied and pasted from eclipse, is here :
public class Test2 {
    public static void main(String[] args) {
    }
}
I am guessing that there is some hidden character in the code which I can't see.
I tried restarting Eclipse, and cleaning the project, but the error is still there!
Even if I copy and paste this code from SO into eclipse the error is still there!
what is going on here?
EDIT
Its not Eclipse! In Netbeans I get the same error :
Also, the original problem occurred when I tried to copy and paste code examples from this webpage http://blue-walrus.com/2011/05/advanced-swing-multithreading-tutorial/
I just did the following:
Cut and paste the code from your Question to a file and compiled it using javac (Java 8). No compilation errors.
Opened Eclipse (Kepler), created a Test2 class, copied the code from the Question to it, and saved it. No compilation errors.
My tentative conclusion is that there is nothing wrong with the java code. (No funky hidden characters, no nasty homoglyphs, no messed up character encodings.) The most likely cause of these weird errors is that your Eclipse session is messed up:
Try restarting Eclipse.
Try closing and opening the Project.
Try loading your code into a fresh workspace.
In extremis ... try a fresh install of Eclipse (same version as before ...)
Following on from my comments your project must have a valid JDK and JRE defined
If these are missing from your project, go to Window > Preferences> Java Installed JREs and click Add..
If you make any changes select Project > Clean... again
To remove these strange hidden empty space characters, I copy and pasted into Outlook Email, and then copy and pasted into Notepad++, and then copied into Eclipse. All fixed :)
Seems you need to copy and paste situations like this through a few different apps, to 'scrub' the text of any funny characters.
I think the java class path is not set in your computer if that is the case go to my computer->properties->Advanced System settings->Advanced tab click Environment variables find variable path & check if the java class path is there. If its not there i suggest you to copy the java class path & edit variable path & paste the url followed by ";" & then restart the eclipse.
better to check your project build path i.e, JRE system library version/is that set or not in the eclipse
way to go for properties section of eclipse :
right click on project - > choose properties ->build path->Libraries->Add Library Variable->JRE system library->click Add alternate JRE radio button->and select your machine jre->click finish->OK
after that go for java compiler in the same window (left side)->check the enable project settings->and choose your java version from drop down box->click Apply->OK
finally save your program and run it
if it again fails you need to check whether you are setting the classpath in proper or not, for this
please refer this link to set classpath :Link
ZWSP and other non-printing characters
Means a Zero-Width-Space character. You are using UTF8 and some characters have a width of 0 so you can not see them.
Change the Encoding to ANSI and back will solve your Problem.

Eclipse - "main cannot be resolved or is not a field" (Android App programming)

I've got a problem with Eclipse. I use the ADT to develop Android apps directly in Eclipse. But right after creating a new project, I got an error in my "MainActivity.java"-file.
I googled and everywhere its said that I have to import an ".R" class from my project folder, which didnt work for me. I also completetely redownloaded Eclipse+ADT in one package this morning.
The line which gives the error is the following:
setContentView(R.layout.main);
Without the "import com.example.myapp.R" statement I've got the same error. Cleaning and rebuilding also didnt have any effect.
Do you have any ideas?
Thanks!
Try to type R r; in your code. Hover your mouse over the 'R' and Eclipse will say "R cannot be resolved to a type". Click import R (com.example.myapp). If this doesn't work then check for the folder gen to say Generated Java Files. Also check to see if they have the classes inside.
R.layout.main refers to a layout XML file in your "res/layout" directory. This seems like the layout file you are trying to reference is named something else.
.
You just need to make sure that your referencing the correct layout in your onCreate method.
setContentView(R.layout.activity_main);
Post the main.xml. Make sure you import your application's R file and not android.R. Restart eclipse, do a clean up by going to your menu Project-> Clean , refresh and tell us if it is fixed.

How to add images in res folder in android

I am trying to add some images to res folder in my android 4.2.2 application. The problem is the project runs fine until whenever I do add, upon running it gives an error
R cannot be resolved to a variable
in the src/MainActiviy.java and the R.java file deletes it self from the gen folder.
Now I'm naming the folder drawable-mdp and every png image is also lower cased. But it doesn't seem to bother.
Thanks!
You are getting this error because R.java failed to create. This happens when you try to a non-png image or the name of image contains some special character or characters in uppercase.
Ok guys I did try a few things and I finally got it. You can't use - hyphen while naming the image files themselves however _ underscore can be used.
Note you always have to use drawable- while naming the folder can't use _ underscore

IntelliJ does not show 'Class' when we right click and select 'New'

We're creating a new project in IntelliJ and must have something wrong because when we right click on a directory, select New and then get the context menu, Java based options are not shown. Currently get things like File, some HTML options, XML options.
We have assumed so far it's something we haven't configured correctly in the Project/Module configuration. The new module we are talking about is part of a multi module project. We created it using a Maven web archetype.
Any help configuring the project nature?
Edit: The answer is basic: 'That moment when you realise somethings not working because you haven't been clicking 'Apply'... :) We had a good laugh at ourselves when we discovered this'
The directory or one of the parent directories must be marked as Source Root (In this case, it appears in blue).
If this is not the case, right click your root source directory -> Mark As -> Source Root.
This can also happen if your package name is invalid.
For example, if your "package" is com.my-company (which is not a valid Java package name due to the dash), IntelliJ will prevent you from creating a Java Class in that package.
you need to mark your directory as source root (right click on the parent directory)
and then compile the plugin (it is important )
as result you will be able to add classes and more
If you open your module settings (F4) you can nominate which paths contain 'source'. Intellij will then mark these directories in blue and allow you to add classes etc.
In a similar fashion you can highlight test directories for unit tests.
Project Structure->Modules->{Your Module}->Sources->{Click the folder named java in src/main}->click the blue button which img is a blue folder,then you should see the right box contains new item(Source Folders).All be done;
I will share another interesting point. If you try to create a package with the reserved keyword then it will be treated as a normal directory and not a package. I was having this issue where I was creating a package named import and it was converting that to a directory.
Another possible solution is that the project name is not acceptable. For example, creating a project with spaces in the name does not block the project creation but the proper sources are not marked and when those are marked manually, I still was unable to create classes. Recreating the project with hyphens (-) instead of spaces corrected the problem for me.
Make sure you are not creating a package name which is same as predefined keywords in java like enum, int, long etc.
In my case I was trying to create a class under "enum" package. As soon as I changed package name to "enums" I was able to create class in it.
Had this issue too. Invalidating Caches/Restart did the trick for me. Please upvote so the the IntelliJ folks take this more seriously. This gives the IDE a terrible UI/UX experience.
https://youtrack.jetbrains.com/issue/IDEA-203100
There is another case where 'Java Class' don't show, maybe some reserved words exist in the package name, for example:
com.liuyong.package.case
com.liuyong.import.package
It's the same reason as #kuporific 's answer: the package name is invalid.
If you just created your project, let IntelliJ finish indexing your project.
You need to mark your java directory as Source Root ,
Right Click on Java directory
Select Mark Directory as option and click on the sub menu option Source Root
Most of the people already gave the answer but this one is just for making someone's life easier.
TL;DR
You must add the test folder as source.
Right click on java directory under test
Mark it as Tests
Add src/test/java in Test Source Folders
Thats it, IntelliJ will consider them as test source.

raw folder in Android

I am trying to play sound effects in a application. Everywhere I look, I am being told to add a raw folder in the res directory(working in eclipse).
First, I add the raw folder under res. Then I put my .wav files inside. When I try to use R.raw.filename in my code, I get an error "raw cannot be resolved". So I clean and rebuild the project. Suddenly, everywhere that I used R.id or R.anything is underlined as an error. When I looked up the error, it turns out that it had something to do with Android.R. Nothing I did could fix it. So I removed the raw folder and rebuilt the project. All of a sudden, everything is fine.
I've repeated this process about 4 times and I have no idea why it's such a pain to add the raw folder. Can anyone tell me what I'm doing wrong?
Do any of your .wav files contains invalid characters in their name? Check if maybe the following messages are shown somewhere in your build:
Unparsed aapt error(s)! Check the console for output.
and
Invalid file name: must contain only [a-z0-9_.]
Only lowercase characters, digits, underscore and period are valid for the names plus extensions.
Use assets folder and ...
InputStream sound = getAssets().open("filename.mp3");
... or raw folder and ...
InputStream sound = getResources().openRawResource(R.raw.filename);
Just make sure that the file called filename exists in the directory called raw.
If all uses of R.* are underlined, delete the import android.R in the upper part of your class as Maxim mentioned.
Apparently your activity added import android.R, after that all references with R goes to android framework (android.jar) which doesn't know anything about your raw files. Just check imports and if you see android.R, delete it and add com.my_app_namespace.R Should work after that
I am not sure if this counts as a solution, but when I used .ogg files instead of .wav files, everything worked perfectly.

Categories

Resources