Home > Software engineering >  ActivityMapsBinding doesn't work, it doesn't let me import the class - Cannot resolve symb
ActivityMapsBinding doesn't work, it doesn't let me import the class - Cannot resolve symb

Time:04-27

I am trying to get google maps to work in my app the moment I deafult google maps via android studio it comes out in red ActivityMapsBinding and then it doesn't work as I should? I've already put addictions and things like that but it doesn't work

public class TrovamicoActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private ActivityMapsBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMapsBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

CodePudding user response:

<layout>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TrovamicoActivity">

      

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

CodePudding user response:

There are few things you can do to solve this error,

First

Make sure there are buildFeatures added in build.gradle(module:), if not then add like this,

buildFeatures {
     viewBinding true 
     dataBinding true
}

Then sync. And after that rebuild your project and wait for a while.

Second

Invalidate caches & restart then rebuild your project and wait for a while.

Third

It might seem a little weird but if you had made any changes in your activity file, undo those changes, come back to your code file, the error will be gone then go back to xml and redo the changes. Error will not show again. (Yeah, I know, Android Studio works in mysterious ways. XD)

Fourth

Make sure your activity's xml file is named activity_maps. If it is named maps_actitviy then binding file will be MapsActivityBinding.

These are the solutions I myself go for whenever I face such issue in my code.

If none of them works then just recreate the whole project.

  • Related