当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 'cannot resolve symbol R' in Android Studio

问题描述:

I just finished my app, and when I try to save it as an APK, this error

appears, all the 'R's in the code turned red.

This is not the first time it happens.

How do I fix it?

  1. This are all the line with errors in the code, all the R. are in red.:

    setContentView(R.layout.dise);

    final EditText e=(EditText)findViewById(R.id.escribiraqui);

    TextView T=(TextView)findViewById(R.id.traduccion);

    Button TRAD=(Button) findViewById(R.id.traducir);

    getMenuInflater().inflate(R.menu.java, menu);

    if (id == R.id.action_settings) {

  2. I tried importing android.R, and now the R isn't red anymore, now the error

    is after the R.id... or R.layout... or R.menu...

    All my variables are in red now, with the same error:

    "cannot resolve symbol 'dise'", etc.

3.I just did alt+Enter on the R, then clicked on Create a class 'R'...

The error disappeared, is this correct or it could lead to another error?

网友答案:

the R gets red when there is a problem generating this file. This can e.g. happen when you have mistakes in your grade-files. I would suggest to open a console in the path of your project and type:

gradle clean build

this should show you the root-cause - and if this works out fine then the red should be gone ( you might have to sync )

网友答案:

do not fiddle with any code , this "R getting red" issue is very common(and all is to do with gradle), I had it a lot in the past but with new gradle and sdk it is much less nowadays, but for anyone has the same issue do as follow hope it will work for you too,

This worked for me:

  1. Close and reopen project
  2. Press " Sync Project with Gradle file " (is next to AVD manager icon in top menu)

repeat 1-2 if Gradle could not Sync for first time. (mine worked in second time).

source, http://stackoverflow.com/a/31127514/4173238

网友答案:

You can rebuild the project by going to build -> rebuild. If that doesn't work, make sure that the names of all assets are lowercase. That really got me, because it prevents the rebuild.

网友答案:

This seems to be a problem for Gradle settings, check your Gradle configuration in the build.gradle file Also make sure to go to

Preferences-> Build,Execution and Deployment -> Gradle -> use gradle default wrapper

Then, Clean and Rebuild your project

分享给朋友:
您可能感兴趣的文章:
随机阅读: