Generated source files should not be edited? - java

When I create this new class Quake and try to write some code in it ,it displays the following message,
and when I try to run it Android Studio automatically delete the class Quake.
Can someone explain me why is this happening and how to resolve it ?

You're in Packages view and putting all your files in the wrong folder. Switch to Project view (left hand, top side toggle). Under app, you should see a folder called src -> main -> java -> com.example.prateek. That's where all your class files should properly be to avoid autodeletion.

This happens due to incorrect Source folder mappings done by some mistake.
In order to resolve the same, please go to
Project Structure -> Project Settings -> Modules.
Select the Module that has this issue and select the Sources Tab
On the Right Hand Side you will find Add Content Root pane. Please Check if the Source and Test Folders are mapping to the correct root and there is no other mapping present apart from the valid root mapping [For eg src for Source Folders and tst for Test Source Folders.]
Delete any unwanted mapping present in either of them. Apply and Re sync the project.
Issue should be solved now.

Related

Is this the correct way of writing sourceSets for multiple folders of the same type?

I used to have all my files in one folder of each type but recently I found an option do it in multiple (at least I think so) - orig. question stackoverflow
I tried to do it the way they did in the answer but there was a note on android site that you should avoid specifying a directory which is a parent to one. And so I did the following:
The code doesnt fail grandle sync but I am not sure if it works because even if I change the name of "mipmap" to something that isnt in the res folder it will sync anyways..
Is this the correct way of writing sourceSets for multiple folders of the same type?
Should I add the srcDirs add to each directory in res folder meaning font/layout/menu/raw/values
Is this the correct way of setting a source directory for mipmap which has multiple qualifiers?
Thanks for any help
EDIT : #MEETPrajapati This error popped in one of the drawable files once I synced it.
You can make a new drawable folder inside new res folder called "module1" like this :
sourceSets{
main {
java.srcDir("src/main/java")
res.setSrcDirs(["src/main/res","src/other/module1"])
manifest.srcFile("src/main/AndroidManifest.xml")
}
}

Renaming Android Package name IOException

I am trying to resolve and invalid package name currently my package looks like this:
-com
--domain
---app
----domain
so the package name is com.domain.app.domain
It needs to be com.domain.app, I have tried to move the contents of ----domain, into ---app but I get an IOException and am told certain directories cannot be moved?
I first get warnings Multiple directories correspond to package, these directories will be moved and all references changed, I select 'Yes', I then select refactor and I see the refactoring preview, I select do refactor and I get java.io.IOException: Cannot move 'and the a reference to the first directory here'.
How else can I resolve this, if I cannot move the files or delete the package?
I think you would find it quicker/simpler to move them manually and replace the package names using a 'Find/Replace in file' tool:
use the file manager/explorer to move the contents of com/domain/app/domain into com/domain/app and delete the now empty 'domain' leaf directory
use the find/replace in files function (in any decent text editor) to replace
'package com.domain.app.domain' with 'package com.domain.app' in all your source files.
change the applicationId in app/build.gradle & AndroidManifest.xml (if you want to - they don't need to be identical to the actual package name in the sources)
Get back to what you actually wanted to do ...
I've always found that the refactor in AS more trouble than it's worth.

Accessing Images in Eclipse

I'm a teacher attempting to use the AP Computer Science Picture Lab activity. Here are the teacher instructions:
Students should keep the images folder and the classes folder together in the pixLab folder.
The FileChooser expects the images to be in a folder called images, at the same level as the classes folder.
If it does not find the images there it also looks in the same folder as the class files that are executing.
If you wish to modify this, change the FileChooser.java class to specify the folder where the pictures are stored. For example, if you want to store the images in “r://student/images/,” change the following line in the method getMediaDirectory() in FileChooser.java:
URL fileURL = new URL(classURL,"../images/");
And modify it to
URL fileURL = new URL("r://student/images/");
I have created a GitHub repo for them to fork and use in Eclipse, but I'm having trouble getting the images in the right place for Eclipse to see them. Where should they be in the Eclipse Package Explorer? The tree now is:
PixLab > src > default package > various classes.
At what level should I drag and drop the images folder into?
Alternatively, what should I the following line to read?
URL fileURL = new URL(classURL,"../images/");
I'm wondering if there is some confusion between the naming of the project in Eclipse with the folder name for the source files.
The directory structure that you give does not include a pixLab folder. I'd be expecting something more like the following in Eclipse:
PixLab > src > pixLab > various classes
> images > various images
Then, the line:
URL fileURL = new URL(classURL,"../images/");
makes sense, as you are going up one level from the classes folder, and down from there to the images folder.
For the reference to a "student" folder, I think they are considering the scenario where the images are stored in a shared network file folder. In that case, they would NOT be part of the Eclipse package, and the FileChooser Url would have to be modified to reflect your chosen network location for the images.
If you've already set up the project in Eclipse with the "default package", can I suggest doing the following:
From within Eclipse:
1) make a package, named pixLab
2) drag & drop files from the default package to the new package
Eclipse should automatically add the following line to the top of all the source files that you brought over:
package pixLab;
3) place the images folder under src, parallel to the pixLab package.
A "package" functions as a folder. If everything is in the same folder, then the rest of the code should work fine.
Thanks for the attempt, but I'm afraid Java still can't find the jpg images. I'm afraid I'm not yet allowed to post images, but I took a screenshot, and the files are laid out exactly as suggested above. Any other ideas, anyone?

eclipse java can not find xml file

i am currently having a problem regarding an xml file while programming in eclipse android.
the problem is that it seems to ask for a folder called R within my project but such a folder was not created on creation of the project.
i have had a look on the internet and review other questions but i cant quite seems to find a solution.
i am trying to set an xml file for a list and this is all the code that should be needed but it can not find the .xml file at all as it seems to want a folder called R to contain it.
new ArrayAdapter<Object>(this,R.layout.edit_item_layout , theList);
anyhelp and suggestion will be well received
UPDATE of useful information
The weird part is it can find activity_main.xml which is in the same folder
the file is located in res/layout
ok i cleaned and now it is giving a list of files i can change it to but the file itself doesn't seem to be in the R.java file ad it is asking me to change it to different files but not the one i want
R is automatically generated, and you should never add or delete from this file.
I have faced this problem many times, and it mostly caused by xml file.
You should check your XML file carefully. It is most likely that you will find your answer there.
If your importing any library, make sure you're importing those file correctly.
R refers your res folder. You should have a subfolder in res called layout that contains your edit_item_layout.xml.
If you're receiving the error R cannot be resolved to a variable, you'll need to:
import your_package_name.R;
If you're receiving the error edit_item_layout cannot be resolved or is not a field, try:
Cleaning your project by selecting: Project --> Clean
Right clicking your project and selecting: Refresh
If you're R.java file is corrupted, because it's automatically generated you can also delete your gen folder and it should be replaced immediately.

R cannot be resolved to a variable [duplicate]

This question already has answers here:
"R cannot be resolved to a variable"? [duplicate]
(30 answers)
Closed 2 years ago.
I got all of this code from here
I am extremely new to all of this, and am having a few problems.
The error "R cannot be resolved to a variable" comes up on the bold parts of this code.
Resources r = context.getResources();
**setBackgroundColor(r.getColor(R.color.candidate_background));
mColorNormal = r.getColor(R.color.candidate_normal);
mColorRecommended = r.getColor(R.color.candidate_recommended);
mColorOther = r.getColor(R.color.candidate_other);
mVerticalPadding = r.getDimensionPixelSize(R.dimen.candidate_vertical_padding);**
mPaint = new Paint();
mPaint.setColor(mColorNormal);
mPaint.setAntiAlias(true);
**mPaint.setTextSize(r.getDimensionPixelSize(R.dimen.candidate_font_height));**
mPaint.setStrokeWidth(0);
The only thing I can think of is that I haven't copied and pasted everything correctly?
R is a Java class file that basically maintains an index of resources by assigning every resource that your application uses with an integer value for lookup. If you look in the res directory of the sample you're following, you'll see a values directory underneath it containing a colors.xml file that defines any custom colors. Note that in this specific example, you are using a color resource, not a Color State List resource as Paul had mentioned. Color state resources go in the color directory, but definitions of simple colors go in the values directory. Confusing, I know.
Resources in Android are defined via XML files. R.java is created automatically by the Android build process when these XML files are parsed. Different types of resources need to be placed in to specific directories for R to be structured properly; quite unhelpfully, not all of these directories are automatically created for you when you make a new project under an IDE.
Read this article about Application Resources, this article about providing resources, and this article about accessing resources (all Google documentation) for a bit of a better understanding.
Lastly, if you DO have all of your XML files in the proper place, check your import statements. Sometimes conflicts can arise if you have too many imports or if all of your imports aren't in place; if you're using the official Android Dev Tools in the form of the Eclipse plugin, the easiest way to handle imports is to use the Ctrl+Shift+O shortcut (Cmd+Shift+O on a Mac). If your imports are correct, and all of your XML files are in place, perform a rebuild on the project. If this still doesn't work and you are using Eclipse, I know that sometimes Resource resolution conflicts can inexplicably be fixed by simply closing Eclipse and launching it again.
R is an automatically generated file made from your resources.
Taken from Android docs
Color State List Resource
Define a color resources that changes
based on the View state. Saved
in res/color/ and accessed from the R.color class.
I would guess that the different R.color.candidate_* all need to be defined under res/color/
The R.java file should be automatically generated by Android. Try the following:
If you are using Eclipse try "project clean" to regenerate the file.
Use Ctrl-Shift-O to "reorganize" your imports. This will automatically add the R.java file to your imports if necessary.
Try to fix all errors not related to the R file and then retry the "project clean" and Ctrl-Shift-O option again. Other errors (e.g. typos in your xml layout files) can "stall" a new build of the R file via project clean.
Make sure your project is an Android project and you have an android.jar file on your classpath. If not fix this and retry step 1-3
you ned to copy the resources om the example you are using into the releavent directories in you res folder. The first word (after R.) is the directory you use.
so for R.color you put the resoruce in res/color in your project.
for the xample the resources are here:
http://developer.android.com/resources/samples/SoftKeyboard/res/index.html
they should also be in the version of the android SDK you downloaded:
ADKPATH/samples/android-/SoftKeyboard/
I got this error message many times. And mostly all of them were caused by some mistake in xml files or that I forgot to put 'check' in front of Build Automatically option. Try it! Maybe it will help You.

Categories