Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R cannot be resolved to a variable

Tags:

android

I would like to fix this error

R cannot be resolved to a variable

I looked up many answers, but I could not get the right one; I tried everything. Could any one help me?

My main activity which is automatically created. The error is showing for the three lines starting at case R.id.button1::

package de.vogella.android.temprature1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class Convert extends Activity {
    private EditText text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (EditText) findViewById(R.id.editText1);
    }

    // This method is called at button click because we assigned the name to the
    // "On Click property" of the button
    public void myClickHandler(View view) {
    switch (view.getId()) {
        case R.id.button1:
            RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
            RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
            if (text.getText().length() == 0) {
                Toast.makeText(this, "Please enter a valid number",
                Toast.LENGTH_LONG).show();
                return;
            }

            float inputValue = Float.parseFloat(text.getText().toString());
            if (celsiusButton.isChecked()) {
                text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
            } else {
                text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
            }

            // Switch to the other button
            if (fahrenheitButton.isChecked()) {
                fahrenheitButton.setChecked(false);
                celsiusButton.setChecked(true);
            } else {
                fahrenheitButton.setChecked(true);
                celsiusButton.setChecked(false);
            }
            break;
        }
    }

    // Converts to celsius
    private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
    }

    // Converts to fahrenheit
    private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
    }
}

my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/myColor">
    <EditText android:layout_height="wrap_content" android:id="@+id/editText1"
        android:layout_width="match_parent" android:inputType="numberDecimal|numberSigned"></EditText>
    <RadioGroup android:layout_height="wrap_content" android:id="@+id/radioGroup1"
        android:layout_width="match_parent">
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio0" android:layout_height="wrap_content"
            android:text="@string/celsius" android:checked="true"></RadioButton>
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio1" android:layout_height="wrap_content"
            android:text="@string/fahrenheit"></RadioButton>
    </RadioGroup>
    <Button android:id="@+id/button1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/calc"
        android:onClick="myClickHandler"></Button>
</LinearLayout>

my string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Convert!</string>
    <string name="app_name">de.vogella.android.temprature1</string>
<string name="myClickHandler">myClickHandler</string>
<string name="celsius">to Celsius</string>
<string name="farenheit">to Farenheit</string>
<string name="calc">Calculate</string>
</resources>
like image 667
zobot1 Avatar asked Apr 08 '11 08:04

zobot1


4 Answers

1) see if the xml files in the Package Explorer tree have an error-icon if they do, fix errors and R-class will be generated.

example hint: FILL_PARENT was renamed MATCH_PARENT in API Level 8; so if you're targeting platform 2.1 (api 7), change all occurences back to "fill_parent"

2) if you added "import android.R;" trying to do a quick-fix, remove that line

like image 144
ivan Avatar answered Oct 21 '22 01:10

ivan


for me, the best solution to suggest to people that have problems with the "R" file would be to try the next steps (order doesn't matter) :

  1. update ADT & SDK , Eclipse and Java (both 1.6 and the latest one).

    EDIT: now the ADT supports Java 1.7 . link here.

  2. remove gen folder , and create it again .

  3. do a clean-project.

  4. right click the project and choose android-tools -> fix-project-properties .

  5. right click the project and choose properties -> java-build-path -> order-and-export. make sure the order is :

    • Android 4.4 (always the latest version)

    • Android private libraries

    • android dependencies

    • your library project/s if needed

    • yourAppProject/gen

    • yourAppProject/src

  6. make sure all files in the res folder's subfolders have names that are ok : only lowercase letters, digits and underscore ("_") .

  7. always make sure the targetSdk is pointed to the latest API (currently 19) , and set it in the project.properties file

  8. try to run Lint and read its warnings. they might help you out.

  9. make sure your project compiles on 1.6 java and not a different version.

  10. restart eclipse.

like image 25
android developer Avatar answered Oct 21 '22 02:10

android developer


Check your AndroidManifest.xml file, see if the package name is correct. If you copied an example it is likely this is wrong and the source of your problem.

like image 33
theCleverBulldog Avatar answered Oct 21 '22 00:10

theCleverBulldog


I had the same problem. I didn't have any lines stating "import something.R". Cleaning and rebuilding also didn't solve the issue.

It turned out to be because of the name of a layout xml file. Those files can only contain lowercase letters, or numbers, or underscores. When I changed the filename (and everything that refers to it) from myLayout.xml to my_layout.xml, everything worked.

like image 36
davidmarkscott Avatar answered Oct 21 '22 02:10

davidmarkscott