Cannot resolve symbol 'ActivityCompat' and 'content'
Asked Answered
L

5

8

I am new to android and was learning how to implement Location Based Services, and i encounter the following errors -

Cannot resolve symbol 'ActivityCompat' in line 'import android.support.v4.app.ActivityCompat;'

Cannot resolve symbol 'content' in the line 'import android.support.v4.content.ContextCompat;'

After doing some research I found out you need to add the following to the gradle.build file compile 'com.android.support:support-v4:23.0.0'

So I did that, But I still get the same errors. I tried clean still nothing.

How do i fix the errors?

This is the link from where i am trying to follow

https://www.tutorialspoint.com/android/android_location_based_services.htm#

This is my java file -

package com.example.location;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.content.pm.PackageManager;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat; # error
import android.support.v4.app.ActivityCompat;    #error
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

// Remaining code

This is my gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.location"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:support-compat:+'
    implementation 'com.android.support:appcompat-v7:+'

    implementation 'com.android.support:support-v4:26.1.0'
}
'''
Limemann answered 10/9, 2019 at 9:55 Comment(0)
S
30

You have to use androidx classes:

Add the dependency:

implementation "androidx.core:core-ktx:+"

and change your import:

import android.support.v4.app.ActivityCompat -> import androidx.core.app.ActivityCompat
import android.support.v4.content.ContextCompat -> import androidx.core.content.ContextCompat

Also remove in your build.gradle the support libraries dependencies:

//implementation 'com.android.support:support-compat:+'
//implementation 'com.android.support:appcompat-v7:+'
//implementation 'com.android.support:support-v4:26.1.0'
Simmer answered 10/9, 2019 at 9:58 Comment(4)
Please look at https://developer.android.com/jetpack/androidx/migrate/class-mappings to find classes from android.support.v4.content namespace and corresponding androidx classes.Orthopterous
Great tip. In my case, however, it creates "Configuration :myApp:debugRuntimeClasspath contains AndroidX dependencies, but the android.useAndroidX property is not enabled, which may cause runtime issues. Set android.useAndroidX=true in the gradle.properties file and retry." -- but... there is no gradle.properties in my Android Studio environment whatsoever! There is only gradle-wrapper.properties and it doesn't seem to solve the issue. Where do I place that android.useAndroidX=true?Helicline
Also, latest & greatest (2021.3.1.17) Android Studio doesn't like androidx.core:core-ktx:+ and it directs to use androidx.core:core-ktx:1.9.0 instead.Helicline
@Helicline just create a file gradle.properties in the root folder and put android.useAndroidX=true inside this file.Simmer
C
9

remove this

import android.support.v4.content.ContextCompat; # error
import android.support.v4.app.ActivityCompat;    #error

and add line as below because you are using androidx

import androidx.appcompat.app.AppCompatActivity;

or after just removing above two lines just click alt+enter on AppCompatActivity

Critic answered 10/9, 2019 at 9:59 Comment(1)
in androidx it not supports v4 or other versions so you need to import packages of 'androidx'Critic
M
6

For react-native

Run command in the project folder.

npx jetify
Misestimate answered 25/6, 2021 at 12:17 Comment(0)
F
3

this is working on my side for AndroidX

build.gradle

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

}

LocationActivity

package com.example.location;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
Frustum answered 23/1, 2020 at 3:5 Comment(0)
S
1

Since you are using androidx, you have to add this line.

import androidx.core.app.ActivityCompat;
Sessile answered 15/7, 2022 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.