Rickard
Rickard

Reputation: 1299

R.java has been generated, still getting 'cannot be resolved to be a variable'

I'm thinking it might have something to do with packages, but I can't find the error. There are no errors(red marks) in my xml files, and all my drawables and xml files conform to the naming convention. I have tried cleaning the project, and there are no import R.java anywhere.

The classes that cannot find R.java is:

  com.datafetcher.main.Menu.java
  com.datafetcher.showperson.MainActivity.java
  com.datafetcher.showperson.DisplayMessageActivity.java

In the Android Manifest I have declared:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.datafetcher"
  ...

And the activities are declared:

<activity
        android:name=".main.Menu"
        android:label="@string/title_activity_display_message">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <activity
        android:name=".showperson.MainActivity"
        android:label="@string/app_name" 
        android:parentActivityName="com.datafetcher.main.Menu" >


    </activity>

    <activity
        android:name=".showperson.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.datafetcher.main.Menu" >

    </activity>

My R.java is generated as gen/com.datafetcher.R.java. Why can't I use it in my code?

This is a nice 5 second edit. Am I actually supposed to import com.datafetcher.R? I just tried for some reason, and it works. I have never done this before, I assumed it was done implicitly somehow.

Upvotes: 1

Views: 621

Answers (5)

cha
cha

Reputation: 1

I had the same issue and it was because I had forgot to add the gen folder to the build path.

Just right click on the gen folder and go "Build Path" > "Use as Source Folder"

Upvotes: 0

kaushik
kaushik

Reputation: 933

just clean your project from project tab than built again your project and remove import android

Upvotes: -1

18446744073709551615
18446744073709551615

Reputation: 16832

Sometimes this happens when you import R from a different package. For example, you copy text from one project in Eclipse, with the com.example.foo package, and Eclipse inserts an import directive for that package's R. But the generated R belongs to another package. In this case you have to delete the bad import.

By the way, do you see all resource IDs not found or only some of them? (In the same file, in different files?)

One more possibility is that your application package and activity package are different. In this case you need and explicit import statement.

In your case, the classes com.datafetcher.R and com.datafetcher.main.Menu belong to different packages, namely, com.datafetcher and com.datafetcher.main. In Java, classes see other classes in the same package without explicit importing, classes in different packages are visible only after you import them.

Upvotes: 1

Rickard
Rickard

Reputation: 1299

One solution to this is to import the R.java file explicitly. With this example it would be:

import com.datafetcher.R;

wherever you need to access it.

Upvotes: 2

Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

I have come across this problem very much ... usually cleaning or building the project does take care of this.

And there are also times when certain file names or refernces like for e.g. ic-launcher etc are not accepted by the general naming convention, (here - is not accepted while naming a file). There are also cases while copying a code may trigger this, so do check the code because it my be importing classes that are not found in your project.

That's mainly what causes R to get corrupt. I hope that answers your problem.

Upvotes: 0

Related Questions