0

I created a android app with eclipse but when I try to execute I've this problem.

package com.example.myfirstapp;

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

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

In the instruction where I use "R" like : setContentView(R.layout.activity_main); builder tell me an error and exactly "R cannot be resolved to a variable"

I've read something about this but I haven't found my solution. It's like R is android.R and not my layout folder. I don't know what to do! I need a little hint. This is really my first android App.

djf
  • 6,592
  • 6
  • 44
  • 62
  • ctrl+shift+O ...then select the R.java of your project – stinepike Jun 29 '13 at 13:32
  • http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error?rq=1 – Kirit Vaghela Jun 29 '13 at 13:33
  • Please refer http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error?rq=1 –  Jun 29 '13 at 13:33
  • Usually Resource file isn't automatically generated if there are errors in your .xml files so, do a double check in your `res/layout/activity_main.xml` layout :) – Cob013 Jun 29 '13 at 13:35
  • If you click on it, it will give a few default solutions, in your case maybe importing com.example.myfirstapp.R would work. – Mdlc Jun 29 '13 at 14:15

5 Answers5

1

If you have android.R in the import lines, delete it. Then, to import the appropiate class:

  • Ctrl + Shift + o (Windows)
  • Cmd + Shift + o (Mac)

If you get a window due to a conflict, choose the R class of your project.

Dhanesh Budhrani
  • 190
  • 3
  • 15
0

Have you tried doing Project-> Clean? That might fix your problem

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
0

Make sure that the res/ directory doesn't have any compilation errors, which will prevent the IDE from producing a valid R.java file, which is used to resolve the R symbol.

Lion
  • 965
  • 10
  • 21
0

best way to resolve this problem, you should use small alpha-bate latter and underscore when you give the file name in drawable folder , res folder , or when paste the image in drawable folder always the name of the images only in small latter . Then clean the project .

Rishi Gautam
  • 1,948
  • 3
  • 21
  • 31
0

I literally tried everything but nothing worked until I went to Window/Android SDK Manager and I installed the SDK Build tools. I then cleaned my app, waited few seconds and the errors disappeared.

Maher Manoubi
  • 585
  • 1
  • 6
  • 18