0

I would like to display a larger view for MapFragment with AlertDialog when click on main MapFragment in acticity. For first click is working nicely, but after pressed Back Button to dismiss, for second click it makes activity stop working.

public void initImageMap() {
    Glide.with(ScheduleStatus.this).load(photoUrl).placeholder(R.mipmap.ic_launcher).into(ivPhotoPicture);

    OnMapReadyCallback onMapReadyCallback = new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.clear();
            googleMap.addMarker(new MarkerOptions().position(mainLatLng).title(markerTitle));
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mainLatLng, 15));

            googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(final LatLng latLng) {
                    adbMap = new AlertDialog.Builder(ScheduleStatus.this);
                    LayoutInflater inflaterMap = getLayoutInflater();

                    View dialogViewMap = inflaterMap.inflate(R.layout.map_fragment, null);

                    adbMap.setView(dialogViewMap);

                    LinearLayout llMap = dialogViewMap.findViewById(R.id.llMap);
                    SupportMapFragment largeMapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.fragmentMap);
                    largeMapFragment.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(final GoogleMap googleMap) {
                            googleMap.addMarker(new MarkerOptions().position(mainLatLng).title(markerTitle));
                            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mainLatLng, 15));

                            googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                                @Override
                                public void onMapClick(LatLng latLng) {
                                    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mainLatLng, 15));
                                }
                            });
                        }
                    });

                    alertMap = adbMap.create();
                    alertMap.show();
                }
            });
        }
    };

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.fMap);
    mapFragment.getMapAsync(onMapReadyCallback);
}

map_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/fragmentMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

From the logcat:

android.view.InflateException: Binary XML file line #7 in com.example.tcscontrolsmanagementver1:layout/map_fragment: Binary XML file line #7 in com.example.tcscontrolsmanagementver1:layout/map_fragment: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #7 in com.example.tcscontrolsmanagementver1:layout/map_fragment: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f080097, tag null, or parent id 0x7f0800ba with another fragment for com.google.android.gms.maps.SupportMapFragment

I don't think the problem is from the map_fragment.xml, the main MapFragment in Activity and the first click on it work nice. I think the problem is about AlertDialog.dismiss(), I tried to check the Builder and View by if else but still not working, can anyone teach me how to check it?

  • Check this: https://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi – Saurabh Thorat Aug 19 '19 at 06:13
  • Don't use a plain `Dialog` to display a `Fragment`. You'll need to use a `DialogFragment`. Your `SupportMapFragment` will then be accessible using its child `FragmentManager`; i.e., its `getChildFragmentManager()`, instead of the `Activity`'s `getSupportFragmentManager()`. – Mike M. Aug 19 '19 at 06:19
  • Load fragment in frame layout instead of direct use of fragment. – Jaymin Aug 19 '19 at 07:26

0 Answers0