Cannot find R.layout.activity_main

后端 未结 29 2312
轮回少年
轮回少年 2020-12-01 01:58

I was trying to work out with multiple layout using some online tutorials. The thing is my program was building and compiling correctly as long as there was only one XML fil

29条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 02:36

    in 2019 I faced the same problem and I searched the internet and found the following which I am sharing with all.

    In android studio letter R stands for the resources and this error occurs because of the build process not able to sync resources with your project. In other words, this error is caused when Android Studio can’t generate your R.java file correctly. This problem happens when you shift code to another system or while building the android project for the first time. So when you create a new activity or new class you will see an error message like “cannot resolve symbol r” with a red underline.

    Below you can find the possible ways to fix cannot resolve symbol r in android studio.

    Update Project Gradle To Latest Version Always use the latest version of Gradle to work android studio properly.

    Sync Project With Gradle File Once you update the Gradle plugin you need to sync project with the Gradle file. Open android studio and click on Files > Sync Project with Gradle Files option.

    Clean and Rebuild Project The most effective solution is the simplest: clean and rebuild your project. Select Build > Clean Project from the Android Studio toolbar, wait a few moments, and then build your project by selecting Build > Rebuild Project.

    Invalidate Caches / Restart If you encounter this error after moving some files and directories around, then it’s possible that the R.layout error is being caused by a mismatch between Android Studio’s cache and your project’s current layout. If you suspect this may be the case, then select File > Invalidate Caches / Restart > Invalidate and Restart from Android Studio’s toolbar. Issues with the names of your resources can also prevent the R.java file from being created correctly, so check that you don't have multiple resources with the same name and that none of your file names contain invalid characters. Android Studio only supports lowercase a-z, 0-9, full stops and underscores, and a single invalid character can cause an R.layout error across your entire project, even if you don’t actually use this resource anywhere in your project!

    My problem and its solution: In my case, I applied all the above but could not solve the problem. Thus I started a new project and pasted my code one by one and validated my code with running the app. Finally, at one point when I first deleted the code in colors.xml and copied and pasted code below in colors.xml file, I got the error.

    #26ae90
    #2e3237
    #428bca
    #ffffff
    #222222
    #999999
    #888888
    #3b4148
    #5e6266
    #26ae90
    #eceef1
    #333333
    #ff6861
    

    when I undo my changes the error vanished again. Thus my code in colors.xml is not the code above and the code already in colors.xml i.e

    #008577
    #00574B
    #D81B60
    #26ae90
    #2e3237
    #428bca
    #ffffff
    #222222
    #999999
    #888888
    #3b4148
    #5e6266
    #26ae90
    #eceef1
    #333333
    #ff6861
    

    "Maybe its because i did not have color primary defined" Hope it will help someone like me who is new in programming.

提交回复
热议问题