[英]Cannot resolve symbol 'ActivityCompat' and 'content'
I am new to android and was learning how to implement Location Based Services, and i encounter the following errors -我是 android 新手,正在学习如何实现基于位置的服务,但遇到以下错误 -
Cannot resolve symbol 'ActivityCompat' in line 'import android.support.v4.app.ActivityCompat;'
无法解析“import android.support.v4.app.ActivityCompat;”行中的符号“ActivityCompat”
Cannot resolve symbol 'content' in the line 'import android.support.v4.content.ContextCompat;'
无法解析“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'
在做了一些研究后,我发现您需要将以下内容添加到 gradle.build 文件
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# https://www.tutorialspoint.com/android/android_location_based_services.htm#
This is my java file -这是我的java文件-
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'
}
'''
You have to use androidx classes:你必须使用androidx类:
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:还要在您的
build.gradle
中删除支持库依赖项:
//implementation 'com.android.support:support-compat:+'
//implementation 'com.android.support:appcompat-v7:+'
//implementation 'com.android.support:support-v4:26.1.0'
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
并添加如下行,因为您使用的是
androidx
import androidx.appcompat.app.AppCompatActivity;
or after just removing above two lines just click alt+enter
on AppCompatActivity
或仅删除以上两行后,只需在
AppCompatActivity
上单击alt+enter
For react-native对于反应原生
Run command in the project folder.在项目文件夹中运行命令。
npx jetify
this is working on my side for AndroidX这适用于我的AndroidX
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
} }
package com.example.location;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
由于您使用的是 androidx,因此您必须添加此行。
import androidx.core.app.ActivityCompat;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.