Google Maps不能在所有设备上使用,而不能在其他设备上使用。我该怎么办?

时间:2019-08-23 13:43:47

标签: java android-studio

我在游戏商店中有一个应用程序,该应用程序在Google地图上使用了一个片段。一切工作正常,但突然之间该片段无法在所有设备上正常工作。例如,对于Samsung Galaxy S8和Xiaomi Mi Max,它可以正常工作,但对于Samsung打开地图

,Galaxy A7(2018)崩溃

我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.infractal.user.mikripoli.MapMain"
xmlns:app="http://schemas.android.com/apk/res-auto">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottom_bar"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="0dp"
    tools:context=".MapsActivity" />

<RelativeLayout
    android:id="@+id/bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/BottomNavView_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fd7901"
        app:menu="@menu/bottom_navigation_bar_without_back">

    </android.support.design.widget.BottomNavigationView>
</RelativeLayout>

我的Java类

  public class MapActivity extends AppCompatActivity implements 
  OnMapReadyCallback {


@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}



@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        hideSystemUI();
    }
}

private void hideSystemUI() {
    // Enables regular immersive mode.
    // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
    // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE
                    // Set the content to appear under the system bars so 
               that the
                    // content doesn't resize when the system bars hide 
             and show.
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // Hide the nav bar and status bar
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}

 // Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | 
  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | 
 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}


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

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.getUiSettings().setZoomControlsEnabled(true);


    CustomInfoWindowAdapter adapter = new 
   CustomInfoWindowAdapter(MapActivity.this);
    mMap.setInfoWindowAdapter(adapter);

    //Watersports Antonis
    MarkerOptions watersportsAntonis = new MarkerOptions();

    watersportsAntonis.position(new LatLng(Double.valueOf(36.260148), 
    Double.valueOf(28.166458)))
            .title("Watersports Antonis")
            .snippet("")

    .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin));

    mMap.addMarker(watersportsAntonis).showInfoWindow();


    //other activity data transfer
    Bundle bundle = getIntent().getParcelableExtra("bundle");
    LatLng fromPosition = bundle.getParcelable("from_position");
    LatLng coordinates = fromPosition;
    mMap.moveCamera(CameraUpdateFactory.newLatLng(fromPosition));
    //mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 
            16));




    if (mLocationPermissionGranted) {
        getDeviceLocation();



        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != 
            PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != 
           PackageManager.PERMISSION_GRANTED) {
            return;
        }


        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);

        CameraUpdate zoom= CameraUpdateFactory.zoomTo(15);
        mMap.moveCamera(zoom);
        mMap.animateCamera(zoom);
}

}
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;
private static final float DEFAULT_ZOOM = 15f;
private boolean mLocationPermissionGranted = false;
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationProviderClient;


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


    getLocationPermission();

    //Bottom Navigation Bar
    BottomNavigationView bottomNavigationView = (BottomNavigationView) 
     findViewById(R.id.BottomNavView_bar);
    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
    Menu menu = bottomNavigationView.getMenu();
    android.view.MenuItem menuItem = menu.getItem(0);
    menuItem.setChecked(true);

    bottomNavigationView.setOnNavigationItemSelectedListener(new 
     BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {

                case R.id.ic_home:
                    Intent intent1 = new Intent(MapActivity.this, 
              MainMenu.class);
                    startActivity(intent1);
                    finish();
                    break;



                case R.id.ic_contact_us:
                    Intent intent3 = new Intent(MapActivity.this, 
                Mail.class);
                    startActivity(intent3);
                    finish();
                    break;
            }


            return false;
        }

      });
  }
 //device location
  private void getDeviceLocation(){

    mFusedLocationProviderClient = LocationServices.
            getFusedLocationProviderClient(this);

    try {
        if (mLocationPermissionGranted){

            Task location = 
      mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                   if (task.isSuccessful()){
                       Location currentLocation = (Location) 
        task.getResult();


                   }
                   else {
                       Toast.makeText(MapActivity.this, "unable to get  
         current location", Toast.LENGTH_SHORT).show();
                   }
                }
            });
        }
    } catch (SecurityException e){
        Log.e(TAG, "getDeviceLocation: SecurityExpection:" +e.getMessage() 
 );
    }

}



private void initMap(){
    SupportMapFragment mapFragment = (SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map);

    mapFragment.getMapAsync(MapActivity.this);
}

private void getLocationPermission(){
    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){
            mLocationPermissionGranted = true;
            initMap();
        }
        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) {
    mLocationPermissionGranted= 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) {
                        mLocationPermissionGranted = false;
                        return;
                    }
                }

                mLocationPermissionGranted = true;

                initMap();
              }
          }
      }
  }

@Override
public void onBackPressed() {
    Intent intent2 = new Intent(MapActivity.this, MapMain.class);
    startActivity(intent2);
    finish();
        }
     }

google play控制台给了我这个错误

java.lang.NoClassDefFoundError

at gv.b (com.google.android.gms.dynamite_mapsdynamite@18385081@18.3.85 (100400-262677519):3)


  at gw.a (com.google.android.gms.dynamite_mapsdynamite@18385081@18.3.85 (100400-262677519):3)


  at gy.a (com.google.android.gms.dynamite_mapsdynamite@18385081@18.3.85 (100400-262677519):14)


  at com.google.maps.api.android.lib6.drd.aj.a (com.google.android.gms.dynamite_mapsdynamite@18385081@18.3.85 (100400-262677519):6)


  at ga.a (com.google.android.gms.dynamite_mapsdynamite@18385081@18.3.85 (100400-262677519):22)


  at ga.run (com.google.android.gms.dynamite_mapsdynamite@18385081@18.3.85 (100400-262677519):8)
Caused by: java.lang.ClassNotFoundException: 


  at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:134)


  at java.lang.ClassLoader.loadClass (ClassLoader.java:379)


  at ad.loadClass (com.google.android.gms.dynamite_dynamiteloader@18385081@18.3.85 (100400-262677519):4)


  at java.lang.ClassLoader.loadClass (ClassLoader.java:312)

1 个答案:

答案 0 :(得分:1)

如果您使用的是{strong> 16.1.0 以下的Google Maps SDK版本,请尝试在清单中添加以下内容:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

引用here