R cannot be resolved to a variable

后端 未结 29 1066
不知归路
不知归路 2020-11-28 14:14

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;

相关标签:
29条回答
  • 2020-11-28 14:59

    Before you try anything else, please check that your package declaration in your AndroidManifest.xml file matches the actual package name in your project's src folder! This has caused this error many times for me when creating test projects which I copy from existing projects. Hope this saves you time!

    Also, creating an XML file with any capital letters will trigger this error.

    0 讨论(0)
  • 2020-11-28 15:00
    import com.de.vogella.android.temprature1.R;
    
    0 讨论(0)
  • 2020-11-28 15:03

    i'm new to android development. I had the same problem and revolved around this thread for about a week. It turned out that as you download latest Android SDKs like API 23 which has recently been released, or if you update build tools, you need to modify the build.gradle file under Gradle Scripts if it does not get updated. It is located in app folder of your Project directory. You can open this file with notepad. My file now looks like this.

        apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.0"
    
        defaultConfig {
            applicationId "com.example.sukhwinder.justjava"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile 'com.android.support:appcompat-v7:23.+'
        compile fileTree(dir: 'libs', include: ['*.jar'])
    }
    

    Make sure compileSdkVersion, buildToolsVersion and targetSdkVersion match the latest version of Android SDK and Build Tools installed on your machine.

    0 讨论(0)
  • 2020-11-28 15:04

    Sooo many different possible solutions! For me, I found that Eclipse had created the faulty file in the wrong package. I fixed it by moving it into the same package as the R.java file.

    0 讨论(0)
  • 2020-11-28 15:04

    I had the same problem. I fix it by deleting the file in 'gen' directory, then I clean the project then the R.java was automatically generated.

    0 讨论(0)
  • 2020-11-28 15:05

    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

    0 讨论(0)
提交回复
热议问题