Reputation: 59
It's my first time working with android studio and I get error
"Cannot resolve symbol AppCompatActivity", and other symbol errors.
How can I resolve it? I've tried adding things to gradle but nothing workde. 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
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'
}
Upvotes: 4
Views: 7253
Reputation: 144
Recently I encountered similar problem - "Cannot resolve symbol AppCompatActivity", although I am using
import androidx.appcompat.app.AppCompatActivity
Of course I migrated to androidx long time ago. 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.
The only thing that helped was shift to Android Studio 4.2 Canary 12 from 4.0.1. Although the topic is old, I wanted to support people encountered this error recently.
Upvotes: 1
Reputation: 3265
As you are using the AndroidX
dependency
androidx.appcompat:appcompat:1.0.2
And in MainActivity
you import the
import android.support.v7.app.AppCompatActivity;
remove it and import the androidx AppCompatActivity
then the error resolved.
Additional
Also, please add com.google.android.material:material:1.0.0
into the build.gradle
Upvotes: 1
Reputation: 363707
You are using androidx then you can remove this line in your 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
Upvotes: 0
Reputation: 2746
You cannot user support lib packages and AndroidX packages together. So if you have migrated your project to android x you will need to use that library only.
Upvotes: 0
Reputation: 149
Remove this two lines from import part and try
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
Upvotes: 0