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);
}
});
}
}
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.
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.
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
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.
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.
It seems like Gradle is taking too long time to execute for your computer. Wait for the Gradle to finish building the project.
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.
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):
- Uninstall from installer. (via control panel for windows)
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.
Remove the SDK
To delete any remains of the SDK, go to %LOCALAPPDATA% and delete the Android directory.
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.
来源:https://stackoverflow.com/questions/26573456/cannot-resolve-symbol-r