google Map in fragment DexIndexOverflowException error

depp

Hey guys i was trying to use google map in a fragment and i saw few tutorials and they all were using fragment activity not fragment.So i tried using the same code in fragment.but i am getting this error Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

below is my code of the fragment

  public class MapsActivity extends Fragment implements OnMapReadyCallback {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
MapView mapView;
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationClient;
private OnFragmentInteractionListener mListener;

.............some fixed code in fragment (havent changed anything)

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view=  inflater.inflate(R.layout.fragment_maps, container, false);
    mapView = (MapView)view.findViewById(R.id.MapView);

    mapView.onCreate(savedInstanceState);
    mapView.onResume();
    mapView.getMapAsync(this);
  //  SupportMapFragment mapFragment = 
  (SupportMapFragment)getChildFragmentManager()
 //           .findFragmentById(R.id.MapView);
 //   mapFragment.getMapAsync(this);

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_maps, container, false);
}

..................(some code i havent touched this part)


LatLng myPosition;
@Override
public void onMapReady(final GoogleMap googleMap) {

    mMap = googleMap;
    mFusedLocationClient = 
  LocationServices.getFusedLocationProviderClient(getContext());

    // Add a marker in Sydney and move the camera
    if (ActivityCompat.checkSelfPermission(getContext(), 
   android.Manifest.permission.ACCESS_FINE_LOCATION) != 
  PackageManager.PERMISSION_GRANTED && Activi 
   tyCompat.checkSelfPermission(getContext(), 
 android.Manifest.permission.ACCESS_COARSE_LOCATION) != 
    PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling

        return;
    }
    Task task= mFusedLocationClient.getLastLocation()
            .addOnSuccessListener((Executor) this, new 
  OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this 
  can be null.
                //    Toast.makeText(getBaseContext(),"code run 
   here",Toast.LENGTH_SHORT).show();
                    if (location != null) {
                        double latitude = location.getLatitude();
                        double longitude = location.getLongitude();
                        String a,b;
                        a= String.valueOf(latitude);
                        b= String.valueOf(longitude);

                        Log.d(a,b);
                  //      
    Toast.makeText(getBaseContext(),a+b,Toast.LENGTH_SHORT).show();
                        LatLng latLng = new LatLng(latitude, longitude);
                        myPosition = new LatLng(latitude, longitude);
                        googleMap.addMarker(new 
  MarkerOptions().position(myPosition).title("Start"));

  mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
                    }
                }
            });
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
} 

i have tried doing this but still the same results

 public View onCreateView(LayoutInflater inflater, ViewGroup 
  container,
                         Bundle savedInstanceState) {

    View view=  inflater.inflate(R.layout.fragment_maps, container, 
 false);


    return inflater.inflate(R.layout.fragment_maps, container, false);
  }


  @Override
  public void onViewCreated(View view,  Bundle savedInstanceState) {




    super.onViewCreated(view, savedInstanceState);
    mapView = (MapView)view.findViewById(R.id.Map);
    if(mapView != null){
        mapView.onCreate(null);
        mapView.onResume();
        mapView.getMapAsync(this);
       }
   }

laayout file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jimmy.workmen.DashboardFragment.MapsActivity">

<!-- TODO: Update blank fragment layout -->

<com.google.android.gms.maps.MapView
    android:id="@+id/Map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

depp

the problem was that i was using

compile 'com.google.android.gms:play-services:7.8.0' 

instead of using location alone it imports whole api so i replaced it by code below and it was working fine

compile 'com.google.android.gms:play-services-location:7.8.0'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Google Map - Error inflating class fragment

Android: Google map error inflating class fragment

Google map fragment within a fragment

Call google map in Fragment

Google map inside another fragment - Error when calling getMapAsync

Binary XML file line #9: Error inflating class fragment with Google Map Fragment

Nesting Google Map fragment inside custom Fragment

Adding google map (fragment?) to activity

Android Google map not working in a fragment

Error : Binary XML file line #2: Error inflating class fragment in Google Map V2

Error inflating class fragment- Android Google Map Api V2

Importing Google Play Services causes DexIndexOverflowException

Error on implementing fragment of google maps

Google map showing on fragment on top of activity

Google Map not showing on tab fragment in ViewPager

Not able to show Google Map inside the fragment

Google Maps fragment shows up but there is no map

Inflate Exception in Google Map Fragment in ViewPager with TabLayout

Cannot load Google map in Kotlin Fragment?

google map in fragment activity in Android studio

App is crashing while using Google map in Fragment

Google map V2 in fragment crash

Loading Android Google Map fragment managed by ViewPager

Google map called inside a fragment returns null

Fragment transaction with animation blacks out google map

Google map view corrupt with extend Fragment Class

Google map fragment not fitting in my custom background

SlidingUp panel in map fragment similar to google maps

Show a google map inside a dialogfragment in a fragment