1

I get this compiler error: Cannot resolve symbol 'R'. I know this question may seem familiar but many other questions related to this problem said that the code could compile despite the error while mine doesn't, or that the problem could be solved by cleaning the project and/or restarting Android Studio (I use 0.8.6), while in my case that doesn't work. Import class solves the compiler error but causes my app to crash when I start the activity. The code of the .java containing the error is:

import android.app.Activity;
import android.os.Bundle;



/**
 * Created by Me on 16-11-2014.
 */
public class TutorialOne extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial1);
    }
}

I have also read that this may be caused by an error in the layout. Therefore I also include the code of the layout, which shows no compiler errors and is a pure pre-made layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

Furthermore, Im following this tutorial: https://www.youtube.com/watch?v=AN5EFviJRR0&list=PLB03EA9545DD188C3&index=10 which uses an old version of Eclipse, can this be causing some kind of problem?

Thanks and sorry if this is still considered a duplicate post

--SOLVED--

Im just stupid and, when creating TutorialOne as class, I accidently created the class in the map java instead of com.something.something.

J03L
  • 314
  • 3
  • 17
Marty_01
  • 179
  • 1
  • 1
  • 10
  • 1
    The import for R is missing. – SME_Dev Nov 17 '14 at 11:46
  • I tried that, but that causes my app to crash when i start the activity, also, in other .java files I never had to do that and they would work fine. – Marty_01 Nov 17 '14 at 11:48
  • 1
    1. Maybe your imported R file wasn't addressed by the correct package/namespace. It should be imported from a package, that has a similar name to your TutorialOne activity. Note, that this is possible only if the build process was successful. Therefore you should check, if there's a R.class file in the project folder. 2. The java classes only need to import the R file, if they want to address UI-Elements, Layouts, resources etc... 3. Is the TutorialOne activity published in the android_manifest.xml? – SME_Dev Nov 17 '14 at 11:53
  • 1
    I'm vaguely recalling that "R" is a class in an auto-generated Java file associated with resource strings. If you dig around you will find R.java and you can read the source to understand what's going on. (I think it uses your default package name so you usually don't need to explicitly import it, but you do if you have different package names.) – Hot Licks Nov 17 '14 at 12:24
  • Im sorry, I should have mentioned it, but I am new to android and java programming. I thought that R was part of the java syntax. 1) Where can I find the R.class file? Do you mean under app=>build=>generated=>source->r? And what should I do with it? 2) Isn't that the case here? Also in another class where I use the same syntax to refer to another layout I have no problems with R at all, without importing anything. 3) yes, although android:name=".TutorialOne" has an error in it. Is this causing the R bug or is the R bug caused by the error in the manifest file? I thought it was the first. – Marty_01 Nov 17 '14 at 13:47

2 Answers2

2

Check the generated build file R.java. Check the classes and package names.

There might be an issue with it. Rebuild the project from Project > Clean and then select the project you want to clean up. This will build your project and create a new bin folder and regenerate the R.java file.

Rishi Dua
  • 2,296
  • 2
  • 24
  • 35
1

I had a similar issue with my Android Studio, the way i fixed mine was by changing the compileSdkVersion to the latest version my studio has. eg.

compileSdkVersion 21

buildToolsVersion '20.0.0'

Hope this helps someone.

Community
  • 1
  • 1
n4zg
  • 387
  • 2
  • 6
  • 20