简体   繁体   中英

Problems linking R.id.x in Android Studio

I've lurked on StackOverflow for years, but now that I'm getting serious about Android development, I've finally started an account.

I Google searched thoroughly for help with my problem, to no avail.

I was successfully developing simple apps in Android Studio 1.4 since I first installed the IDE on my old Linux laptop last month. Then, I installed Android Studio 1.5 on a much better machine, because the hardware limitations on the old laptop were really getting to me.

I was careful to install Oracle JDK 7 before installing Android Studio, and also to enable KVM in BIOS. I also made sure to install all of the Android SDKs that weren't specific to Android Auto.

When I started to import my old projects, Gradle was having a hell of a time compiling them. So I decided to start a new project and copy and paste my old code via a text editor.

I learned that I now have to activate permission checking in Java in addition to requesting permissions in my Manifest XML, in order to accommodate Android 6.0 Marshmallow, which is fine.

I put this code into my MainActivity.java like so:

 public class MainActivity extends AppCompatActivity implements LocationListener {

int permissionCheck = ContextCompat.checkSelfPermission(this,
        permission.ACCESS_COARSE_LOCATION);
int permissionCheck2 = ContextCompat.checkSelfPermission(this,
        permission.ACCESS_FINE_LOCATION);

Android Studio is still marking this code in my OnCreate method in red:

Location location = locationManager.getLastKnownLocation(provider);
    onLocationChanged(location);

On mouse cursor hover, I still get: "Call requires permission which may be rejected by user."

I also have another problem.

I made sure to assign the correct ID to the views I'm using from my activity_main.xml to my MainActivity.java. But still, when I call findViewById and use R.id.x, it's not linking and that bit of text isn't purple in my IDE. Here's the code I'm referring to:

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

        latTV = (TextView) findViewById(R.id.lat);
        lngTV = (TextView) findViewById(R.id.lng);
    addressTV = (TextView) findViewById(R.id.address);
    altitudeTV = (TextView) findViewById(R.id.alt);

None of the R.id.x nor the R.layout.activity_main in setContentView are turning purple as they're supposed to. As I said, I made sure that the resources and IDs exist.

OS: Kubuntu 14.10 LTS IDE: Android Studio 1.5

Thanks in advance for your help.

Since you already have this check

boolean permissionCheck = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED);
boolean permissionCheck2 = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED);

The next step would be to execute any statement that requires these permissions only when they are granted.

NOTE : You should have implemented atleast one of the Location Permission (Coarse/Fine)

// Only execute if one of the two type of location permission is granted
if( permissionCheck || permissionCheck2){

  // Code to initialize the locationManager via getSystemService

  Location location = locationManager.getLastKnownLocation(provider);
  onLocationChanged(location);
}

As for your issue with R.id.x , that is there because you copy pasted your code and R.java file is a IDE generated file which as gets populated when ever you create an ID in layout. In your case you copy pasted and the R.java never got populated with the IDs. The solution to this should have been built into the IDE but sadly as of now it is not there.

What you can do as of now is ( and it might sound really useless..but this works , specifically if you copy pasted code..)

  1. Goto your layout and delete a character in the id you have assigned and add it again. In doing so you make the IDE update the id field or in your case add it to the R.java file, hence making it accessible in code
  2. Once you are done updating every other id you have in all your layout files, goto terminal tab there in the bottom left toolbar and run ./gradlew clean build
  3. Once gradle finishes its work you should have a working copy of everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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