地图在 Nexus 5X API 28 中崩溃,但在 Google PIXEL 2 XL API 27 中运行良好
Map crashes in Nexus 5X API 28 but runs perfectly fine in Google PIXEL 2 XL API 27
我已按照 Google 开发人员指南使用 Google 地图的 API 创建地图 activity。我的API密钥已经激活了,请问Nexus 5X崩溃的原因是什么。
共享我的 AndroidManifest.xml、MapActivity.java、google_maps_api.xml、activitymaps.xml 和 Gradle 模块:-
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kiit.mapone">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MapsActivity.java
package com.example.kiit.mapone;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
google_maps_api.xml
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">KEY IS HERE</string>
</resources>
出于安全原因,我已将此文件中的 API 密钥替换为 "KEY IS HERE"
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
build.gradle(模块:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.kiit.mapone"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Logcat(错误):
2018-12-18 10:06:42.509 27581-27610/com.example.kiit.mapone E/AndroidRuntime: FATAL EXCEPTION: Thread-5
Process: com.example.kiit.mapone, PID: 27581
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at ez.b(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at ey.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):6)
at ed.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):21)
at ed.run(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):8)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/user_de/0/com.google.android.gms/app_chimera/m/0000000d/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/0000000d/MapsDynamite.apk!/lib/x86, /system/lib]]
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@14799084@14.7.99 (100700-223214910):4)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at ez.b(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at ey.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):6)
at ed.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):21)
at ed.run(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):8)
2018-12-18 10:06:42.660 27581-27581/com.example.kiit.mapone E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted
在你的manifest里面添加<application>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
编辑
您的应用程序标签应如下所示
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>
我已按照 Google 开发人员指南使用 Google 地图的 API 创建地图 activity。我的API密钥已经激活了,请问Nexus 5X崩溃的原因是什么。
共享我的 AndroidManifest.xml、MapActivity.java、google_maps_api.xml、activitymaps.xml 和 Gradle 模块:-
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kiit.mapone">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MapsActivity.java
package com.example.kiit.mapone;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
google_maps_api.xml
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">KEY IS HERE</string>
</resources>
出于安全原因,我已将此文件中的 API 密钥替换为 "KEY IS HERE"
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
build.gradle(模块:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.kiit.mapone"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Logcat(错误):
2018-12-18 10:06:42.509 27581-27610/com.example.kiit.mapone E/AndroidRuntime: FATAL EXCEPTION: Thread-5
Process: com.example.kiit.mapone, PID: 27581
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at ez.b(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at ey.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):6)
at ed.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):21)
at ed.run(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):8)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/user_de/0/com.google.android.gms/app_chimera/m/0000000d/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/0000000d/MapsDynamite.apk!/lib/x86, /system/lib]]
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@14799084@14.7.99 (100700-223214910):4)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at ez.b(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at ey.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):6)
at ed.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):21)
at ed.run(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):8)
2018-12-18 10:06:42.660 27581-27581/com.example.kiit.mapone E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted
在你的manifest里面添加<application>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
编辑
您的应用程序标签应如下所示
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>