使用 Google 地图 api 的演示 android 应用程序崩溃

Posted

技术标签:

【中文标题】使用 Google 地图 api 的演示 android 应用程序崩溃【英文标题】:Demo android app using Google map api is crashing 【发布时间】:2019-07-03 11:58:04 【问题描述】:

我学习 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 的错误信息吗 很抱歉,我在这里添加了错误信息。 【参考方案1】:

LogCat 有错误,

classnotfoundexception: 没有找到类 "org.apache.http.ProtocolVersion"

搜索那个错误会显示很多这样的结果,试试这个,这可能会解决错误,将它添加到你的 Androidmanifest.xml

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

【讨论】:

【参考方案2】:

您是否忘记添加包名:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.example.googlemapdemo">

    <application
        android:allowBackup="false"

.....

【讨论】:

软件包名称在清单中,如果我没有在问题中发布它,我会犯错误,对不起。我更新了问题中的清单。你介意再看一遍吗?

以上是关于使用 Google 地图 api 的演示 android 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

[转]MBTiles 离线地图演示 - 基于 Google Maps JavaScript API v3 + SQLite

地形高度 Google Map Api v2

添加数千个标记 Google Map API V3

在 maps.google.com 上的缩放比在 Google Maps API v3 上更流畅

高德地图车机版API演示程序

使用Javascript从Google Places搜索api获取纬度和经度