6

I was working on Android Studio for adding a short toast message.(I was making an Android Wear Application)

I couldn't know why this code has error on 'symbol R' . It says "Cannot Resolve Symbol R."

    import android.app.Activity;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.wearable.view.WatchViewStub;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MyActivity extends Activity {

        private TextView mTextView;

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

    //getnextpage
    Button getnextpage;
    getnextpage = (Button) findViewById(R.id.getnextpage);
    getnextpage.setOnClickListener(new OnclickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Here is 2nd Page", Toast.LENGTH_LONG).show();
        }
    });
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });
  }
 }
Lux
  • 17,835
  • 5
  • 43
  • 73
SparkyM
  • 61
  • 1
  • 1
  • 3
  • Possible duplicate of ["cannot resolve symbol R" in Android Studio](https://stackoverflow.com/questions/17054000/cannot-resolve-symbol-r-in-android-studio) – Zoe Mar 13 '19 at 16:12

8 Answers8

9

Below some procedures are mentioned :

1) Try Build -> Clean/Rebuild project .

2) Manually typing import <package name>.R, If in import R is missing.

3) complete all resources of its corresponding XML file .

I hope any one of them would be helpful for you as there is no Particular way to resolve this issue.

Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
4

If in your case in imports R is missing you Have to import R manually.

import <packagename>.R;

Replace <packagename> with your own package name.

In your code, R was not imported, therefore your Android Studio might got confused to find resources in your package.

Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
JasonYang
  • 686
  • 7
  • 14
  • you mean write the code 'import .R;' on the top? – SparkyM Oct 26 '14 at 13:33
  • Add import .R to your import section of the code, not necessary on the very top, remember to use your own package name in the manifest. – JasonYang Oct 26 '14 at 13:35
  • Well, I tried Shortkey Ctrl+Shift+O , it does not work. – SparkyM Oct 26 '14 at 13:38
  • Check your project see whether you have an error in resource(xml files, especially), in this case, your file will fail to compile, and you cannot resolve R neither. This is another check if you have already imported R. Otherwise, might have to check deeper in the code. – JasonYang Oct 26 '14 at 13:42
2

I know others have said most of these points, but this should serve as a sum-up and hopefully bring up some new solutions!

If only the letter 'R' is red, it means there is something wrong with your R file (found at build\generated\source\r\debug\com\domain.projectname\R). Don't go around editing your R file- because each time the project is cleaned and built/rebuilt the R file regenerates (it completely resets without fail). There are a few possible solutions:

1) You might have written import android.R; at the top of your Activity. You need to have a different R file imported. This means if your domain is "example" and your project name is "project" then your import should be: import com.example.project.R

2) R is a build file. That means it disappears when you clean and generates anew when you build, as previously said above. Make sure you build or rebuild your project after cleaning it.

3) Maybe your gradle project sync has not completed. Wait until all processes have finished running!

4) It is also possible your R file is corrupt or missing. If the above don't work, you can come to this conclusion. It has a very easy fix, because as I explained earlier when you rebuild a new R file is generated. So just navigate to Build and click Clean project, then when that's finished, click Rebuild project. With the new R file your problems should be gone.

P.S: It's nearly impossible to give an accurate answer without all relevant code provided

Rushat Rai
  • 743
  • 2
  • 15
  • 31
0

The thing that finally did the trick for me was looking at the "event log" tab in Android Studio where I saw:

Frameworks detected: Android framework is detected in the project Configure

clicking on "Configure" set the AndroidManifest.xml as configuration-file of the project (identified it as "android") and only then all the "R" errors went away and I was able to compile & run.

Before doing that - all the actions I've tried such as: "clean", "rebuild" and restarting Android studios - didn't work.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

It seems like Gradle is taking too long time to execute for your computer. Wait for the Gradle to finish building the project.

Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
Karue Benson Karue
  • 957
  • 12
  • 26
0

I resolved it by editing my build.gradle file.

The compiledSdkVersion must align with your appcompat dependency.

My compileSdkVersion was 22 so I edited my appcompat dependency to

compile 'com.android.support:appcompat-v7:22.2.1'

So, I hope this can be a solution.

Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
Kudehinbu Oluwaponle
  • 1,045
  • 11
  • 11
0

It happens if your class is missing the link of R.Class which you may have to import. So, Before starting the class you can simply start by

import Your_Package_name.R;

E.g.

import com.example.myproject.R;

You can manually write this when Clean/Rebuild project is not the solution.

Use-case:

import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import com.example.jsonwebservice.R;

This worked for me.

Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
0

Just in case any of the above answers don't work, the following solved the issue in my case...

Here's what I tried:

  • Multiple "cleans", "rebuilds" and "invalidate caches restart"

  • Attempts to manually add [my package].R,

This didn't work, so I uninstalled Android Studio, and reinstalled the latest version.

The accepted answer to the above link has the following steps (which I reproduce here):

  1. Uninstall from installer. (via control panel for windows)
  2. Remove Android Studio files

    Go to your user folder (%USERPROFILE%), and delete .android, .AndroidStudio and any analogous directories with versions on the end, i.e. .AndroidStudio1.2, as well as .gradle and .m2 if they exist. Then go to %APPDATA% and delete the JetBrains directory. Finally, go to C:\Program Files and delete the Android directory.

  3. Remove the SDK

    To delete any remains of the SDK, go to %LOCALAPPDATA% and delete the Android directory.

  4. Delete Android Studio Projects

    Android Studio creates projects in a folder %USERPROFILE%\AndroidStudioProjects, which you may want to delete.

I skipped 4), then uninstalled a separate Java 7 installation I had, for good measure.

Next I downloaded the latest Android Studio (3.3.2) and installed it.

A bit drastic perhaps, but now everything is working and the "Cannot resolve symbol R" error is gone.

grooble
  • 617
  • 1
  • 8
  • 27