0

I want to import Google Api in my project to see google map but it shows error ( java.lang.RuntimeException: Unable to start activity ComponentInfo)

I search a lot related to this question but nothing foung enough related to my problem according to my code struture

Here is my Main Activity named as (GoogleMapLocat)

public class GoogleMapLocat extends AppCompatActivity {
//map data
private static final String TAG = "HomeActivity";
private static final int ERROR_DIALOG_REQUEST = 9001;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_map);

    //map data
    if (isServicesOK()) {
        init();
    }
}
//MAP DATA
public void init() {
    Button btnMap = (Button) findViewById(R.id.btnMap);
    btnMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(GoogleMapLocat.this, MapActivity.class);
            startActivity(intent);
        }
    });

}

public boolean isServicesOK() {
    Log.d(TAG, "isServicesOK: checking google services version");
    int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(GoogleMapLocat.this);
    if (available == ConnectionResult.SUCCESS) {
        Log.d(TAG, "Google Play Services is Working");
        return true;
    } else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)) {
        //an error occured but we can resolve it
        Log.d(TAG, "isServicesOK: an error occured but we can fix it");
        Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(GoogleMapLocat.this, available, ERROR_DIALOG_REQUEST);
        dialog.show();
    } else {
        Toast.makeText(this, "you can,t make map request", Toast.LENGTH_LONG).show();

    }
    return false;
}
}

Here is my Second Activity named as (MapActivity)

 public class MapActivity extends AppCompatActivity implements OnMapReadyCallback{
private static final String TAG = "MapActivity";

private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;

//vars
private Boolean mLocationPermissionsGranted = false;
private GoogleMap mMap;




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    getLocationPermission();
}
@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onMapReady: map is ready");
    mMap = googleMap;
}

private void initMap(){
    Log.d(TAG, "initMap: initializing map");
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    mapFragment.getMapAsync(MapActivity.this);
}

private void getLocationPermission(){
    Log.d(TAG, "getLocationPermission: getting location permissions");
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION};

    if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
            FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            mLocationPermissionsGranted = true;
        }else{
            ActivityCompat.requestPermissions(this,
                    permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }else{
        ActivityCompat.requestPermissions(this,
                permissions,
                LOCATION_PERMISSION_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.d(TAG, "onRequestPermissionsResult: called.");
    mLocationPermissionsGranted = false;

    switch(requestCode){
        case LOCATION_PERMISSION_REQUEST_CODE:{
            if(grantResults.length > 0){
                for(int i = 0; i < grantResults.length; i++){
                    if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                        mLocationPermissionsGranted = false;
                        Log.d(TAG, "onRequestPermissionsResult: permission failed");
                        return;
                    }
                }
                Log.d(TAG, "onRequestPermissionsResult: permission granted");
                mLocationPermissionsGranted = true;
                //initialize our map
                initMap();
            }
        }
    }
}
}

Here is mapactivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

Here is GoogleMapLocat.xml file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GoogleMapLocat">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Map"
    android:id="@+id/btnMap"
    tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is my Stack Trace:

E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_agriculture10/com.example.e_agriculture10.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
  Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
  at com.example.e_agriculture10.MapActivity.onCreate(MapActivity.java:37)
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_agriculture10/com.example.e_agriculture10.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment 
 Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment

According to this line of error

   at com.example.e_agriculture10.MapActivity.onCreate(MapActivity.java:37)

Here is the Affected area

 setContentView(R.layout.activity_map);

because when i click on (MapActivity.java:37) the cursor blinks on the start of this line( setContentView(R.layout.activity_map);)

According to this line of error

 Binary XML file line #6: Error inflating class fragment

here is the affected block of code (as i think this is the key area related to my error)

 <fragment xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

What,s wrong in this fragment which leads to inflate exception?

4
  • which version of com.google.android.gms:play-services-maps are you using? Commented Sep 2, 2019 at 21:11
  • here is the block of code to get google play services version( android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" ) @GabrieleMariotti Commented Sep 2, 2019 at 21:18
  • Get the version in your build.gradle file Commented Sep 2, 2019 at 21:19
  • This is the block of code to get version in build.gradle( implementation 'com.google.android.gms:play-services:11.4.0')@GabrieleMariotti Commented Sep 2, 2019 at 21:23

1 Answer 1

1

Since you are using androidx libraries, you have to use a SupportMapFragment migrated from Android Support Libraries to AndroidX Libraries.

In you case use this version or later.

com.google.android.gms:play-services-maps:17.0.0

If you check the doc it extends androidx.fragment.app.Fragment.

Sign up to request clarification or add additional context in comments.

2 Comments

Thumbs up for you man!!! My error has gone and program excuted perfectly but why still map is not showing in my project?can you guide me please?
You should open a new question. In this way the community can help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.