简体   繁体   English

'无法解析符号 R' android studio 错误

[英]'cannot resolve symbol R' android studio error

Execution failed for task : app:mergeDebugResources .任务执行失败: app:mergeDebugResources What is this error and how to I fix it?这是什么错误以及如何解决?

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
         App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

This is my xml above... and below is onCreate() where setContentView(R.layout.activity_main) is causing the problem:这是我上面的 xml ......下面是onCreate()其中setContentView(R.layout.activity_main)导致问题:

cannot resolve symbol R无法解析符号 R

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    provider = locationManager.getBestProvider(new Criteria(), false);

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

Another solution, If anyone need it另一个解决方案,如果有人需要它

I was using gradle 3.3.0.我使用的是 gradle 3.3.0。 It was the main culprit.这是罪魁祸首。 Wasted 6.50 hours from my life.浪费了我生命中的 6.50 小时。 Gradle 3.2.1 removed the error. Gradle 3.2.1 删除了该错误。

classpath 'com.android.tools.build:gradle:3.2.1'

'cannot resolve symbol R' error usually occurs and the 'symbol R' turns red in color whenever the gradle sync is not completed. 'cannot resolve symbol R' 错误通常会发生,并且每当 gradle 同步未完成时,'symbol R' 会变成红色。 Hence before running any project it is always advisable to first sync the project if any dependencies or anything have been added and then clean the project and then run it.因此,在运行任何项目之前,如果添加了任何依赖项或任何内容,始终建议先同步项目,然后清理项目然后运行它。

It should solve your problem.它应该可以解决您的问题。

R is predefined class in android. R是 android 中的预定义类。 you should clean your project and then build it again.你应该清理你的项目,然后重新构建它。 it will resolve R .它将解决R

Go to the file directory.转到文件目录。

  1. Click "app"点击“应用”
  2. Click "com.example.?????"点击“com.example.???????”
  3. Right Click右键点击
  4. Hover over "New"将鼠标悬停在“新建”上
  5. Click on "Java Class"单击“Java 类”
  6. Name the file "R" and leave the other things blank & accept the defaults将文件命名为“R”并将其他内容留空并接受默认值
  7. Click OK点击确定
    You have finished the first part!你已经完成了第一部分!
  8. Click File单击文件
  9. Hover over "New"将鼠标悬停在“新建”上
  10. Click "New Project"点击“新建项目”
  11. Accept all defaults until you've reached the layout xml file接受所有默认值,直到您到达布局 xml 文件
  12. Click "app"点击“应用”
  13. Click "com.example.android.?????"点击“com.example.android.???????”
  14. Click "R"点击“R”
  15. Select all全选
  16. Go back to your old file回到你的旧文件
  17. Paste粘贴
  18. Finished!完成的!

可能您的 gradle 同步失败,这就是为什么无法解决重新同步 gradle 或您尚未导入yourpackagename.R的原因;

Only this solution worked for me:只有这个解决方案对我有用:

1- Go to your app directory -> /app/src/main/res/ 1-转到您的应用程序目录-> /app/src/main/res/

2- Delete folder XML ( Make sure that you don't have important files inside it ) 2- 删除文件夹 XML(确保里面没有重要文件

Also i just found out if you have used an updated version of android studio like 4.2.2 and you want to go back to android studio 3.2.1 for example... the constraint layouts can also cause issues.此外,我刚刚发现您是否使用了 android studio 的更新版本,例如 4.2.2,并且您想返回到 android studio 3.2.1……约束布局也可能导致问题。 if you delete the constraints to each one like: app:layout_constraintStart_toStartOf="parent" for one example it would successfully sync and this problem would be solved如果您删除每个约束,例如: app:layout_constraintStart_toStartOf="parent" 例如,它将成功同步并且此问题将得到解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM