Android Studio: Gradle: error: cannot find symbol variable

后端 未结 8 538
Happy的楠姐
Happy的楠姐 2020-11-27 20:07

I was working on my app and everything was normal until I tried to display image in java.

I ran the app once and it ran normally, the picture was displayed. After th

相关标签:
8条回答
  • 2020-11-27 20:21

    Another alternative to @TouchBoarder's answer above is that you may also have two layout files with the same name but for different api versions. You should delete the older my_file.xml file

    my_file.xml
    my_file.xml(v21)
    
    0 讨论(0)
  • 2020-11-27 20:24

    make sure that the imported R is not from another module. I had moved a class from a module to the main project, and the R was the one from the module.

    0 讨论(0)
  • 2020-11-27 20:33

    You shouldn't be importing android.R. That should be automatically generated and recognized. This question contains a lot of helpful tips if you get some error referring to R after removing the import.

    Some basic steps after removing the import, if those errors appear:

    • Clean your build, then rebuild
    • Make sure there are no errors or typos in your XML files
    • Make sure your resource names consist of [a-z0-9.]. Capitals or symbols are not allowed for some reason.
    • Perform a Gradle sync (via Tools > Android > Sync Project with Gradle Files)
    0 讨论(0)
  • 2020-11-27 20:34

    If you are using multiple flavors?

    -make sure the resource file is not declared/added both in only one of the flavors and in main.

    Example: a_layout_file.xml file containing the symbol variable(s)

    src:

    flavor1/res/layout/(no file)

    flavor2/res/layout/a_layout_file.xml

    main/res/layout/a_layout_file.xml

    This setup will give the error: cannot find symbol variable, this is because the resource file can only be in both flavors or only in the main.

    0 讨论(0)
  • 2020-11-27 20:36

    If you are using a String build config field in your project, this might be the case:

    buildConfigField "String", "source", "play"
    

    If you declare your String like above it will cause the error to happen. The fix is to change it to:

    buildConfigField "String", "source", "\"play\""
    
    0 讨论(0)
  • 2020-11-27 20:36

    Make sure you have MainActivity and .ScanActivity into your AndroidManifest.xml file:

    <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ScanActivity">
    
        </activity>
    
    0 讨论(0)
提交回复
热议问题