Android Studio 2.1 Cannot resolve symbol 'R' - java

Android Studio 2.1,
Java V8 Update 91, Gradle 2.1.0 .
I know that this questions has been asked many times here, but none of the recommendation solutions works out.
In a sudden after compiling ready to run on Emulator, my IDE give an error at the R.id attribute. Every Java files suddenly give the same error even I don't make any changes to the file.
I tried to make a new project, see if things different but the error keep coming. In my mind it could be internal error, again. Even I just update my IDE just now.
The Java file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science);
list_View = (ListView) findViewById(R.id.maintable);
String[] Days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
dayList.addAll(Arrays.asList(Days));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
list_View.setAdapter(adapter);
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
if (nfc.isEnabled()) {
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
Toast.makeText(this, "NFC turned ON", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Please turn On NFC", Toast.LENGTH_SHORT).show();
}
}
The R is the error.
XML file that associate to the Java;
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/maintable"
>
</ListView>
Is this, again, an internal error?

Try syncing the project, go to Tools > Android > Sync Project with Gradle Files
UPDATE
If you're using Android Studio 3.3, use the Sync Project with Gradle Files icon

I have just gone through this nightmare. Ended up removing all xml code to notepad until it was fixed. Then reintroduced sections of code until problem appeared.
Thinking back, it all started when I pasted some code using WordPad. Apparently WordPad contains a different coding or something so bet to avoid.
Anyway just check your white space in xml by deleting it all to rid yourself of any hidden characters.

Try performing a clean and rebuild by going to Build > Clean Project and Build > Make Project

Sometimes the R.java class is not created when there is some error into an xml file (like activity_main.xml). For example, if you change some attribute name that has a reserved word that cannot be changed.
Like this: if you changed the attribute called: id to another name like
id_A_Word_With_Error_Here in this code that can be part of the activity_main.xml, your error (R class not created) can occur.
<android.support.v7.widget.RecyclerView
android:id="#+id/messageRecyclerView"
and there is an error like:
<android.support.v7.widget.RecyclerView
android:id_A_Word_With_Error_Here="#+id/messageRecyclerView"
If clean, rebuild did not work and there is no error with import like import com.something... , my suggestion is to search an error into xml files.

I've seen this problem many times. The right solution is:
When you type R, the Android Studio will give you many different namespaces for R to choose from, that look like R(***.***.***), but what you really need is the namespace that ends with ***.myProject. (Note: myProject is your project name), e.g: R(com.example.milka.demoProject).
If you choose any other namespace, you will get the error message, so you have to delete the unneeded import, and re-choose your R

I try to change the Gradle version from 2.1.0 to 2.0.0, even in AS ver 2.1, ignoring the Instant Run performance and its work out. The little R error is now gone. Now, I tried to change back to 2.1.0, the R error also gone..!!
Sometime this little thing drive me crazy, a bit.
Shame on me.

Maybe the error may cause for the tools, trying install Android SDK Build Tools from Tools > Android > SDK Manager.
or maybe this error can be caused by adding an activity to a namespace that is different to the root, you can trying >>Import com.example.MyApplication.R;
or your also can clean o rebuild your project in Build > Clean Project or Build > Rebuild Project

If you are using google play services and your compilesdk version is lower than 23 then download the new sdk and update your compile version. After that try cleaning and rebuilding. It will be fixed

Have you tried Rebuild Project go to
Build> Rebuild Project
and perform it without doing the Clean project
I had the same problem & it worked for me

Related

Importing from GitHub to Eclipse

I have actually managed to import a project from GitHub to Eclipse, but my problem is when I try to run the project it keeps saying fix errors on source files. Now I know that the code should work as the person who wrote this app has it in the Google App store. This keeps happening for me on all the projects I have downloaded.
Are the suggestions to what I am doing wrong?
Thanks for the replies, I am new to the whole programming world, so I am not sure whether or not the project has a build file or not.
As #Andres has said, my initial thought was that it was to do with the JDK/Android development kit being not the same as I have on Eclipse.
A link to the project is https://github.com/glebpopov/Hacker-News-Droid-App.
The first error I get says in the console window
Unable to resolve target 'android 7'
I also have this example for the beginning of the class Activity Helper:
public class ActivityHelper {
private static final String TAG = "ActivityHelper";
protected Activity mActivity;
protected SharedPreferences sharedPref = null;
And the error here for the second line of code say:
String cannot be resolved into a type
The error here for the third line of code says:
Activity cannot be resolved into a type
And there are plenty more across the whole code.
Some possible causes are:
The version published on GitHub has errors, and is not the same that is published on the google app store.
The project has dependencies that have not been resolved, because of your Maven (or similar product) configuration.
The jdk and/or android development kit required by the project are not the same as the one you have on your Eclipse

Eclipse shows error (unable to resolve target)

I got a project and imported it in Eclipse (it is an android project).
The project works on other computers, but unfortunately not on mine.
The error which comes up is in the console of eclipse when i import the project:
"Unable to resolve target 'android-8'"
Furthermore do i get multiple errors in on of the .java-files in the src folder of the project.
3 of them are showing "String cannot be resolved to type":
Here the code (It is for a music player the timer /showing of the seconds):
/**
* Function to convert milliseconds time to
* Timer Format
* Hours:Minutes:Seconds
* */
public String milliSecondsToTimer(long milliseconds){
String finalTimerString = "";
String secondsString = "";
I hope you can help me!
Thank you so much in advance!
If you have any questions please let me know.
Thanks guys.
Cheers
You'll be having project.properties file open that and change the android-8 to whatever android version you have. After changing this goto Window->Preferences->Android. You'll be having target name & api level. select any one of them & specify it in project.properties file.
It seems you have something set with a build target of API 8. Perhaps a subproject or library?
If that's intended then it sounds like you need to download Android 2.2 (API 8) through your Android SDK manager.
Right click on the project, Properties -> Android -> Select the build target -> Apply -> OK

Error with R.java class, layout no found but present in R.java

Well, first I'm sorry but my english is not very good because I'm French...
I work on Eclipse on an android project (pong)
So, I've a problem with an activity, when I write : setContentView(R.layout.menu);
I have an error who say : menu cannot be resolved or is not a field
The fact is... This is a file ! I went to the R.java and I see it, but the error don't disappear. (I have alredy clean and build the project many times, it still don't work)
I have took some screens ;-)
http://imageshack.us/photo/my-images/4/erreurmenupong1.png/ (In the Menu.java)
http://imageshack.us/photo/my-images/62/erreurmenupong2.png/ (In the R.java)
Delete "android.R" from your imports.
I had the same exact problem. I solved it buy making a new work space and importing all my android projects over there.

Error: Could not find or load main class in intelliJ IDE

I'm a beginner in Java and am trying to run my code using IntelliJ that I just installed as my IDE with JDK 1.7. The following piece of code keeps does not even compile and keeps giving me the error:
Error: Could not find or load main class libTest
Code
import java.lang.Integer;
import java.lang.String;
import java.lang.System;
import java.util.*;
class book {
private String name = "trial";
private int bookCode=1;
private int issued=0;
public void Issue(){
if(issued==0) {
issued=1;
System.out.println("You have succesfully issued the book");
}
else {
System.out.println("The book is already issued. Please contact the librarian for further details");
}
}
public int checkCode() {
return bookCode;
}
String readName() {
return name;
}
public void setName(String newName){
name=newName;
}
public void setBookCode(int newCode){
bookCode=newCode;
}
}
class library {
private ArrayList books=new ArrayList();
public void getList(){
for(int bk:books){
String bName=books(bk).readName();
System.out.println((bk+1)+") "+bName);
}
}
}
public class libTest{
public static void main(String[] args){
library newLib= new library();
System.out.println("code working");
}
}
Is there any change that i have to make in the compiler settings?? Or is it the code.
This might help:
1) "Build" menu -> "Rebuild Project".
Sometimes Intellij doesn't rewrite the classes because they already exist, this way you ask Intellij to rewrite everything.
2) "Run" menu -> "Edit configuration" -> delete the profile -> add back the profile ("Application" if it's a Java application), choose your main class from the "Main Class" dropdown menu.
3)"Build" menu -> "Rebuild Project".
If none of the above answers worked for you, just close your IntelliJ IDE and remove the IntelliJ IDE file and folder from the root of your project:
rm -rf .idea *.iml
Then open the project with IntelliJ. It must work now.
For me the solution was to fix the output directory under project settings. Before I was using just "target" for the Project compiler output. Instead I updated it to have a full path e.g. D:\dev\sigplusjava2_68\target
I had this problem and I tried everything under the sun that I could think of and on this site.
None of my Java classes were being picked up after I pulled from a remote branch. All the classes had red Js by their names in the Project Hierarchy, not blue Cs.
In the end, I tried to follow this tutorial and a few steps in tried something not described and fixed the issue:
https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
Here's what I did:
Goto File | Project Structure, or press Crtl+Shift+Alt+S
Select Modules under the Project Settings section.
In the Sources tab click Sources on the 'Mark as:' line.
Click the Apply button.
For some reason, all my classes then had blue C's.
Someone with a better understanding of how IntelliJ and/or IDE's might be able to explain the phenomenon, but all I know is now it can see all the classes and more importantly the main one, and run.
Invalidate cache and restart your IntelliJ, it worked for me.
Explicitly creating an out folder and then setting the output path to C:\Users\USERNAME\IdeaProjects\PROJECTNAME\out
seemed to work for me when just out, and expecting IntelliJ to make the folder wouldn't.
Also try having IntelliJ make you a new run configuration:
Find the previous one by clicking
then remove it
and hit okay.
Now, (IMPORTANT STEP) open the class containing your main method. This is probably easiest done by clicking on the class name in the left-hand side Project Pane.
Give 'er a Alt + Shift + F10 and you should get a
Now hit Enter!!
Tadah??
(Did it work?)
File > Project Structure > Modules > Mark "src" folder as sources.
This should fix the problem. Also check latest language is selected so that you don't have to change code or do any config changes.
I know this was asked a while ago, but I was just stumbling over this issue and thought my findings might help others. As pointed out, the error message is basically a result of the out folder. That's because, when you're trying to run the program, it compiles the code first, and puts the compiled result to the out location, and then it tries to load the compiled code from the out location. If the compiled code is not in the location expected, you'll get the error.
The point I'm particularly wanting to share is that some times, the code is not compiled (built), even though your run configuration specifies "Build" in the "Before launch" section of the configuration panel.
When can this happen?
One situation that can cause this to happen is if you're using modules and you manually delete the module out directory. For example, if I have a module named "foo", there should be a directory named foo under out/production. If you manually delete it, the build system may not know that it needs to be rebuilt.
Even worse, if you select Build | Build module 'foo', it still may not rebuild the module. If that's the case, you should select a file in the module, for example 'bar.java' and then select Build | Recompile 'bar.java'. Now the out directory out/production/foo should be restored.
Since IntelliJ typically knows about any changes going on, this surprised me, and took me a little time to figure out, so I thought I'd share.
Check your class module : I have encountered this problem with intellij :
I have a maven multi-module project, the problem is that i runing a class which not exist the module within the configuration, so my problem is fixed by setting the right module ("edit configuration" -> "use class of module")
may this help you
I had to mark the "src" folder as "Sources". After restarting IntelliJ and rebuilding the project I could run the project without further issues (see screenshot).
Edit: You can access the "Project Structure" tab via File->Project Structure or by pressing Ctrl+Shift+Alt+S.
I ran into this problem when my Java class was under src/main/kotlin. After I moved it to src/main/java, the problem was gone.
I have faced such problems when the class is in the default folder, i.e. when the class does not declare a package.
So I guess using a package statement (eg. package org.me.mypackage;) on top of the class should fix it.
Open Modules Tab (Press Ctrl+Shift+Alt+S). I had two modules under one project. I've solved the problem after removing the second redundant module (see screenshot).
After creating your project in intelliJ, try running the following command:
mvn package
I have tried all the hacks suggested here - to no avail. At the end I have simply created a new Maven application and manually copied into it - one by one - the pom.xml and the java files and resources. It all works now. I am new to IntelliJ and totally unimpressed but how easy it is to get it into an unstable state.
Invalidating cache didn't work.
I edited the main class java file with a dummy change and ran it. It worked.
In my case the problem seemed to be related to upgrading IntelliJ. When I did this I overwrote the files from the old IntelliJ with the files from the new IntelliJ (2017 community to 2018 community). After that all of my projects were broken. I tried everything in this thread and none of them worked. I tried upgrading gradle to the latest version (4 to 4.8) and that didn't work. The only thing that worked for me was deleting the entire IntelliJ folder and reinstalling it. All of my projects worked after that.
I have tried almost everything suggested in the answers here, but nothing worked for me.
After an hour of just trying to run my application, I noticed that my project's path included non-ASCII characters (Arabic characters). After I moved my project to a path with no non-ASCII characters, it executed just fine.
Goto File-> Invalidate Caches and Restart .
Else delete rm -rf .idea *.iml
and restart InteliJ
You can run the maven command on the pom.xml file in your project directory:
mvn clean install
For me - i tried few of the options above, did not work. Then i just renamed my Application class and that probably forced intelliJ to build a fresh jar and error message started to change. Then i renamed it back and it worked.
Mark the directory as a source directory. Opened via Ctrl+Shift+Alt+S
modules.xml with wrong content, I don't know what's matter with my IDEA.
I inherited a bunch of .JAVA files from elsewhere and couldn't figure out how to get them to work in any IDE. Ultimately I had to go to the command line where the Main.JAVA file was and run javac Main.java. This created a bunch of .CLASS files. The IDE's were then able to figure out what to do.
I got this error when using Scala/SBT. IntelliJ could not find the main class, even though everything was set up correctly.
My solution: delete the <user>/.sbt/<version>/plugins/target folder, then restart IntelliJ.
You probably would have specified a wrong package and the package hierarchy would not be right. Look below
The ide would highlight the wrong path in that case.
I'm using IntelliJ with Spring and my main class is wrapped in a JAR.
I had to mark the 'Include dependencies with "Provided" scope' in the Run/Debug configuration dialog
We are at File/Project Structure..
Answer might be:
Folder indicated as "content root" needs a child folder where the code is.
Plus find the button that marks code as excluded and not.
Not to be confused with tickbox that states excluded without telling in what phase and what**
Is it compiler exclude or runtime exclude? You are doomed to test and lot.
So no that tickbox but icons and colors.
As an idea we need to crack how it was originally thought to work. They never got it to work in first place and started add things in premature codaculation style.
It has been so many years and you cannot expect any improvement.
But as cure we can hack out some way to get it right every time.
Another thing you can check here is the actual command that is being passed to the JVM and make sure it looks OK. Scroll to the top of your Run console, it should be the first line.
Spaces in your Run Configuration VM Options field will malform the app startup command and can result in this error message
-DsomeArgument="arg with space must be quoted"
I am working with Kotlin but am guessing the problem is the same. I would start a project, create a single file and add main to it and the IDE couldn't find the main.
I tried the things in this list and none worked. I finally mentioned my frustration on one of the IntelliJ pages and was contacted. Of course, it worked fine for IntelliJ. After a couple of days back and forth, I noticed that the highlight function wasn't working and mentioned that. It turned out something was wrong with the IDE settings. I still don't know specifically what was wrong but the fix in my case was to reset the IDE settings. File->Manage IDE Settings->Restore Default settings.
After this, the green triangle start icon became visible to the left of my main function and things continued to work normally for subsequent projects.
Thanks to Konstantin at JetBrain's support for his patience.

R.id cannot be resolved [duplicate]

This question already has answers here:
R cannot be resolved - Android error
(108 answers)
Closed 10 years ago.
So I copied this tutorial example thing right from Google's android site and I ma getting an error that R.id cannot be resolved.
Here is my Java file
package com.TestApp.HelloWebView;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class HelloWebView extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
Here is my res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
You have to import your R class
import com.TestApp.HelloWebView.R;
Also as Demand posted you have to use a namespace for your layout.
?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
I had the same problem when I updated my Android SDK. Suddenly new projects could not generate the R.java file.
importing my R.java didn't help because it wasn't generated
cleaning and building the project didn't help either -- the /gen folder was still empty.
I found that after the upgrade, the adb server didn't start up properly. So a manually started it:
$ cd $ANDROID_SDK/platform-tools
$ adb start-server
Then I restarted Eclipse and did a clean/build on the project, which generated the R.java file.
If using Eclipse, run a clean build (Project/Clean...); this can often clear issues with the resources not being compiled correctly.
Did you try any of the solutions listed here?
R cannot be resolved - Android error
Also, look for your R.java file. Are there any errors with it?
I had this problem too. I can't say a solution in general, but you can make a new workplace and copy the hardcode. In the future never click on clean or build, instead save or start eclipse again.
Go in your main.xml file, delete a char and rewrite it. That will generate the R file again.
On trying an Android beginner's tute (http://mobile.tutsplus.com/tutorials/android/android-sdk-development/) the "R.id cannot be resolved" error had me stumped too. In my case it was an android:onClick in /res/layout/main.xml. It seems onClick is not supported in AVD platform 1.5 API level 3.
A change to a new AVD with platform 1.6 API, level 6 removed the error - also started the project from scratch with the new api and level settings. For absolute Eclipse newbies (like myself) create a new AVD with:
Window -> Android SDK and AVD Manager -> New. Select platform, level number from "Target" drop down list.
Also try deleting the ~.android\debug.keystore (Happened to me, thought it was due to R.id issue). Real problem was debug.keystore was older than a year!
I ran into this problem when I tried to follow Lars Vogel's Android development tutorial...it turns out my problem is simply because I didn't save the layout file. Once I save the file, it works fine.
make sure you do not have any xml files with capitals in the name.
I had the same problem.
all the file names in res (and all its sub folders including layout) must be lower case only. This includes image files, xml files, etc (even if they aren't referenced anywhere within the project.)
if you posted full your main.xml file, then it's not right. You must write a namespace of xml:
xmlns:android="http://schemas.android.com/apk/res/android"
for example:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="78dip"
android:layout_height="65dip"
android:paddingTop="4dip"
android:textSize="12dip"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#color/bright_text_dark_focused"
android:gravity="center_horizontal|center_vertical" />
Yeah another fix
I started the emulator. By running a helloandroid program, that did compile.
And got out of eclispe and got back in don't know which one fixed the problem..
Cleaning the project etc didn't fix it - can i have 2 hrs of my life back!
import com.TestApp.R;
isn't accepted once R is lost.
My Eclipse now auto creates a new "res" folder everytime I "clean". Yes, I have two folders named "res" in parallel directly under my project folder. The auto created one is empty. String values are still found. No other errors than "R cannot be resolved" are reported by Eclipse.
Eureka! I've managed to make my first android project import to Eclipse!
Earlier I always got "Unable to find system library" under the project's Properties - Java build path - Libraries tab and it was impossible to correct it once it occured. The consequence was that no import statements could be resolved by anything imported.
In this case, I changed the JDK compliance level from 1.6 to 1.5 BEFORE I imported (afterwards its too late and cannot be mended). It must be done from Window - Preferences - Java - Compiler (earlier I thought this option was available only by right-clicking on already imported project). Then right-click on each project and set Properties - Android to a suitable Target (with the errors above, eclipse ignores any selection here). Then, I could even change back to JDK compliance level 1.6.
So, in general, changing the JDK compliance level before importing, might help.
And when deleting a project ALWAYS check the "Delete project content on disk (cannot be undone)" checkbox in the "Are you sure" dialog. Otherwise it is impossible to re-import the project, o if you've imported it with the wrong JDK compliance level the first time, it is impossible to correct the mistake, other than to try in another workspace.
These are the experiences of a very frustrated Eclipse user.
Make sure there is not a hidden error in one of your .xml files.
Sometimes you can delete a statement in the strings.xml file that is important.
For example, in the Vogella example, I got the error:
Error: No resource found that matches the given name (at 'title' with value '#string/menu_settings').
I had deleted the line:
<string name="menu_settings">Settings</string>
Putting this back in or deleting res/menu/main.xml would solve the error.
I had the same problem but got fixed
i dont know exactly how this problem gonna fix
Just try, It might be helpful
add this code at the top of ur code
import com.MyFirstApp.activities.R;
here com.MyFirstApp.activties is my component name replace it with your component name

Categories

Resources