Lucreacia
Lucreacia

Reputation: 79

R cannot be resolved to a variable

I have read the other solutions for this. There is no import.filename.R, all of my .xml titles are lower case, I have cleaned the project. I tried recreating it, but none of it has worked. All the "R."s in my file are errored and can't be resolved to a variable. I have five different XML files, so I'll just show a couple of them, and then the java:

activity_main:

<TextView
    android:id="@+id/categoryTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="22dp"
    android:text="@string/cat" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/categoryTextView"
    android:layout_below="@+id/categoryTextView"
    android:layout_marginTop="14dp" >

    <RadioButton
        android:id="@+id/bkRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/bekind" />

    <RadioButton
        android:id="@+id/oldmcRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/oldmc" />


</RadioGroup>

<Button
    android:id="@+id/chooseButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/radioGroup1"
    android:layout_marginTop="18dp"
    android:text="@string/choose"
    android:onClick="buttonclick" />

</RelativeLayout>

bekindwords:

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

<TextView
    android:id="@+id/nounKindTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/noun" />

<EditText
    android:id="@+id/nounKindEditText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10" />

<TextView
    android:id="@+id/nounpKindTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/nounp" />

<EditText
    android:id="@+id/nounpKindEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10" />

<TextView
    android:id="@+id/nounKindTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/noun" />

<EditText
    android:id="@+id/nounKindEditText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10" />

   <TextView
    android:id="@+id/placeKindTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/place" />

<EditText
    android:id="@+id/placeKindEditText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10" />

<TextView
    android:id="@+id/adjKindTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/adj" />

<EditText
    android:id="@+id/@+id/adjKindEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10" />

<TextView
    android:id="@+id/nounKindTextView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/noun" />

<EditText
    android:id="@+id/nounKindEditText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10" />

<Button
    android:id="@+id/enterButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonclick2"
    android:text="@string/ent" />

</LinearLayout>

Java:

package com.deitel.madlibs;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {
//Global variable
    double bk;
    double oldmc;
    private Button choose;  // creates a button 

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        choose = (Button) findViewById(R.id.chooseButton);

        //Start with first screen
        setContentView(R.layout.activity_main);
    }

  //buttonclick for form 1
    public void buttonclick(View view){

        RadioButton bk = (RadioButton) findViewById(R.id.bkRadio);
        RadioButton oldmc = (RadioButton) findViewById(R.id.oldmcRadio); 

         if(bk.isChecked())
        {
            //switch views to screen 2
               setContentView(R.layout.bekindwords);  
        }
   else if(oldmc.isChecked())
        {
           //switch views to screen 3
           setContentView(R.layout.mcdwords);
        }}

    // buttonclick for bekindwords
         public void buttonclick2(View view){
                setContentView(R.layout.bekindadlib);
         }
    // buttonclick for mcdwords
         public void buttonclick3(View view){
                    setContentView(R.layout.mcdadlib);
         }

} 

Upvotes: 1

Views: 809

Answers (4)

Shogun
Shogun

Reputation: 94

I actually just came across this issue today and it had been pestering me for the last hour or so. It took some digging around in my code but eventually I ended up finding the problem. Early on when I first created my project, Eclipse added a blank activity and a few basic resources. One of the resources created was an oddly placed menu.xml file which referenced a string that I had originally deleted when I created the project. Since problems in an XML layout file prevent an application from being built, I simply re-added the string being called, in the string XML value file.

So really the lesson that I learned in solving my issue was that if there is a problem with your R file not being generated, then it is likely due to some problem in an XML file that is preventing your application from being built.

Upvotes: 2

Simon
Simon

Reputation: 14472

I'm adding this in the (forlorn) hope that other people with resource class problems might find this.

There is nothing mysterious about

android.R

, or

your.packagename.com.R

.

When the AAPT compiles your application, it generates R.java. You can see it in the gen folder off the root of your project structure. It simply defines a series of public static final ints that reference the resources in your projects, e.g. drawables, strings, layout views etc so that you may refer to them in your code using the name.

R.java is therefore just a class, like any other Java class. The java file is compiled into your app along with all of the other classes you use.

In turn, it has inner classes for each resource type. For example:

package com.company.example

public final class R {

    public static final class drawable {
        public static final int icon=0x7f020005;   
    } 
}

Here, I have an icon in my drawables named 'icon'. In my code, I can use findViewById(R.drawable.icon).

Look again at the code above and it's obvious what's happening. It compiles as findViewById(0x7f020005);

The problem happens because any number of things can cause the generation of R.java to fail, most commonly errors in layout XMLs. Since R.java is not generated, when your application is compiled, the compiler tries to resolve your references to R.

Consider again findViewById(R.drawable.icon). Your R.java has not been generated but Android has a default resource class at android.R. The compiler will helpfully (or not) resolve the reference for you and adds android.R to your imports. It then tries to compile findViewById(R.drawable.icon), which is effectively findViewById(android.R.drawable.icon) and since the default Android class has no drawable.icon, compilation fails.

To fix it, there are only two things required.

  1. Remove android.R from the imports
  2. Find and fix the problem causing your R.java generation to fail

In my experience, XML errors in layout XML are not always correctly shown in the IDE. When I've got stuck with this, I've ended up going through my version history looking at recent changes and/or opening each layout XML and checking for errors in the preview.

Hope this helps at least someone...

Upvotes: 0

nhaarman
nhaarman

Reputation: 100378

Is R generated at all? If not, you might have problems in your resource files. Fix them, and then do a clean project.

Upvotes: 0

pouzzler
pouzzler

Reputation: 1834

Try importing com.deitel.madlibs.R, cleaning, removing the import. I do something close to it when eclipse bugs on the R, though maybe not exactly that.

Upvotes: 1

Related Questions