片段中的谷歌地图DexIndexOverflow异常错误



嘿,伙计们,我试图在一个片段中使用谷歌地图,我看到了一些教程,他们都在使用片段活动,而不是片段。所以我尝试在fragment中使用相同的代码。但我得到了这个错误错误:任务":app:transformClassesWithDexForDebug"的执行失败。

com.android.build.api.transform.TransformException:com.android.ide.common.prrocess.ProcessException:java.util.concurrent.ExecutionException:com.ndroid.dex.DexIndexOverflowException:方法ID不在[0,0xffffff]中:65536

下面是我的片段代码

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;

片段中的一些固定代码(没有更改任何内容)

@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);
}
} 

我试过这样做,但的结果仍然一样

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);
}
}

布局文件

<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" />

问题是我使用的是

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

它不单独使用location,而是导入整个api所以我用下面的代码替换了它,它运行得很好

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

最新更新