Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'R cannot be resolved to a variable' - gen folder is empty

I've got an error:

R cannot be resolved to a variable

in line:

setContentView(R.layout.main);

but I can't resolve it doing things that other users wrote should be tried, which are:

  1. Getting rid of 'import android.R' line.
  2. Cleaning the project and then building it again.
  3. Closing project and then opening it again.
  4. Going to Project Properties > Java Build Path > Tick Android Version Checkbox
  5. Following the procedure: Uncheck Project->Build Automatically Project->Properties->Builders: Make sure all Android (3) and Java (1) builders are checked (and nothing more) Window->Preferences->Android->Build: Check "Automatically refresh Resources and Assets folder on build" Right-click on project->Android Tools->Fix Project Settings (not sure what it actually does, but it can't hurt)
  6. Some users advice to check if .xml files are correct. I don't know how I could do that. I must admit I'm new to java and android programming, and I haven't even written a single line in my project - the code has been generated automaticly.
  7. Layout xml file is named using lower-case letters.
  8. No error in resource files (no red-crosses).

layout main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

Thanks in advance for your help!

like image 525
Quarra Avatar asked Feb 16 '12 13:02

Quarra


3 Answers

Check your res/ folder in order to be sure that every file it contains has no error. If any error exists R can not be generated.

like image 69
Blackbelt Avatar answered Nov 12 '22 16:11

Blackbelt


Yeah, i got stuck on this for hours. My solution was to: delete all projects from workspace->re-import the desired project by going to file->import->Browse->selecting the project->and finally check the "add project to working sets box" then clicking finish. The .R errors i once had then disappeared. I hope this helps someone!

like image 28
showlove Avatar answered Nov 12 '22 18:11

showlove


Don't import Android.R;

instead,

import package.name.R; where package.name is your package name declared in the manifest.

Android.R contains all the default layouts packaged with Android. Your custom layout will not be Android.R, it will be in package.name.R;

like image 1
edthethird Avatar answered Nov 12 '22 16:11

edthethird