[英]Cannot resolve symbol, android studio
It's my first time working with android studio and I get error这是我第一次使用 android studio,我收到错误
"Cannot resolve symbol AppCompatActivity", and other symbol errors. “无法解析符号 AppCompatActivity”和其他符号错误。
How can I resolve it?我该如何解决? I've tried adding things to gradle but nothing workde.我试过向 gradle 添加东西,但没有任何效果。 Also tried doing "Invalidate caches/restart" but it also didn't work.还尝试执行“使缓存无效/重新启动”,但它也不起作用。
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCall);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1566"));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
}
Here are dependencies in gradle file这是gradle文件中的依赖项
dependencies {
compile 'com.android.support:appcompat-v7'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
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'
}
As you are using the AndroidX
dependency当您使用AndroidX
依赖项时
androidx.appcompat:appcompat:1.0.2
And in MainActivity
you import the在MainActivity
您导入
import android.support.v7.app.AppCompatActivity;
remove it and import the androidx AppCompatActivity
then the error resolved.删除它并导入androidx AppCompatActivity
然后错误解决。
Additional额外的
Also, please add com.google.android.material:material:1.0.0
into the build.gradle
另外,请将com.google.android.material:material:1.0.0
添加到build.gradle
Recently I encountered similar problem - "Cannot resolve symbol AppCompatActivity", although I am using最近我遇到了类似的问题——“无法解析符号 AppCompatActivity”,虽然我正在使用
import androidx.appcompat.app.AppCompatActivity
Of course I migrated to androidx long time ago.当然,我很久以前就迁移到了 androidx。 This happened when I tried to debug my application on real device running Android 10. Tried many things: delete .gradle and .idea folders, Invalidate caches, etc.当我尝试在运行 Android 10 的真实设备上调试我的应用程序时发生了这种情况。尝试了很多事情:删除 .gradle 和 .idea 文件夹、使缓存无效等。
The only thing that helped was shift to Android Studio 4.2 Canary 12 from 4.0.1.唯一有帮助的是从 4.0.1 转移到 Android Studio 4.2 Canary 12。 Although the topic is old, I wanted to support people encountered this error recently.虽然这个话题很老了,但我想支持一下最近遇到这个错误的人。
Remove this two lines from import part and try从导入部分删除这两行并尝试
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
You cannot user support lib packages and AndroidX packages together.您不能同时支持 lib 包和 AndroidX 包。 So if you have migrated your project to android x you will need to use that library only.因此,如果您已将项目迁移到 android x,则只需使用该库。
You are using androidx then you can remove this line in your build.gradle
你正在使用androidx然后你可以在你的build.gradle
删除这一行
compile 'com.android.support:appcompat-v7'
Change the import in your class:更改类中的导入:
//Remove these
//import android.support.v7.app.AppCompatActivity;
//import android.support.v4.app.ActivityCompat;
//Add this
import androidx.appcompat.app.AppCompatActivity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.