java - 使用 Google 地图 api 的演示 android 应用程序崩溃
问题描述
我学习 android 应用程序开发已经有一段时间了,我遇到了这个烦人的错误,我的应用程序在我的设备上运行时崩溃(Mi a1,android 9,API 28)。我没有对原始代码进行任何更改,只是在适当的地方输入了我的 api。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapdemo">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<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">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<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>
google_maps_api.xml
<resources>
<!--
TODO: Before you run your application, you need a Google Maps API key.
To get one, follow this link, follow the directions and press "Create" at the end:
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=D0:2E:31:27:4F:07:82:C0:F9:CD:5F:14:70:FF:28:CB:29:85:5A:CE%3Bcom.example.googlemapdemo
You can also add your credentials to an existing key, using these values:
Package name:
D0:2E:31:27:4F:07:82:C0:F9:CD:5F:14:70:FF:28:CB:29:85:5A:CE
SHA-1 certificate fingerprint:
D0:2E:31:27:4F:07:82:C0:F9:CD:5F:14:70:FF:28:CB:29:85:5A:CE
Alternatively, follow the directions here:
https://developers.google.com/maps/documentation/android/start#get-key
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
-->
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDHi-e0aEeDxZbspTt5m5ydO0Kv5gxdd_8</string>
MapsActivity.java
package com.example.googlemapdemo;
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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@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));
}
}
错误信息 :
E/AndroidRuntime: FATAL EXCEPTION: Thread-6
Process: com.example.googlemapdemo, PID: 26667
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at fb.b(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):3)
at fc.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):6)
at ee.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):23)
at ee.run(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):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/000000bc/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/000000bc/MapsDynamite.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
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@15090083@15.0.90 (100408-231259764):4)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at fb.b(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):3)
at fc.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):6)
at ee.a(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):23)
at ee.run(:com.google.android.gms.dynamite_mapsdynamite@15090083@15.0.90 (100408-231259764):8)
请帮忙。
解决方案
LogCat 有错误,
classnotfoundexception:没有找到类“org.apache.http.ProtocolVersion”
搜索该错误会显示许多类似这样的结果 belov,试试这个,这可能会解决错误,将其添加到您的 Androidmanifest.xml
<application
....
>
...
<uses-library android:name="org.apache.http.legacy"
android:required="false"/>
....
<application/>
推荐阅读
- azure - 如何将结构添加到 Azure IoT Central 自定义应用程序
- mysql - 通过合并的 LEFT JOIN 过滤
- c++ - libSDL2-mixer:如何更改 wav 文件 MixChunk 中的样本?
- node.js - 猫鼬 .exec() 不起作用,但承诺可以
- performance - 多处理器缓存一致性
- scripting - 无法弄清楚如何从 Az Devops CLI 创建一个空的构建管道
- google-app-engine - 外部 MySQL 数据库的 GAE 缓慢
- pandas - 使用 nx.from_pandas_edgelist 创建图
- python - Python - 特殊字符的不同CRC结果
- docker - Jenkins 无法使用 docker:无法连接到 Docker 守护进程