谷歌地图如何与安卓系统协同工作

Posted

技术标签:

【中文标题】谷歌地图如何与安卓系统协同工作【英文标题】:How google map works with android 【发布时间】:2015-07-14 08:04:34 【问题描述】:

三个月后,我仍然无法在我的项目中使用可用的 Google 地图。

我尝试了很多教程,但总是出现同样的问题,我无法运行它,我不知道如何解决它。

这是我的 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmaphmida"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.example.androidmaphmida.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.androidmaphmida.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="com.example.androidmaphmida.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBg-SzhfkXjGUDK037BbmsG4Hv3yrY6c2M" />
         <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.androidmaphmida.MainActivity" />

     <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
      </activity>
    </application>

</manifest>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_
    android:layout_ />

这是我的 MainActivity: 包 com.example.androidmaphmida;

import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;

import com.example.androidmaphmida.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends Activity 

    // Google Map
    GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        //geolocalisation
        map.setMyLocationEnabled(true);
    

    @Override
    protected void onResume() 
        super.onResume();
    

我的日志猫

05-04 18:58:32.773: D/AndroidRuntime(17215): Shutting down VM
05-04 18:58:32.773: W/dalvikvm(17215): threadid=1: thread exiting with uncaught exception (group=0x40bef1f8)
05-04 18:58:32.788: E/AndroidRuntime(17215): FATAL EXCEPTION: main
05-04 18:58:32.788: E/AndroidRuntime(17215): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.androidmaphmida/com.example.androidmaphmida.MainActivity: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.os.Looper.loop(Looper.java:137)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.ActivityThread.main(ActivityThread.java:4514)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at java.lang.reflect.Method.invoke(Method.java:511)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at dalvik.system.NativeStart.main(Native Method)
05-04 18:58:32.788: E/AndroidRuntime(17215): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.Activity.setContentView(Activity.java:1835)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.example.androidmaphmida.MainActivity.onCreate(MainActivity.java:19)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.Activity.performCreate(Activity.java:4465)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-04 18:58:32.788: E/AndroidRuntime(17215):    ... 11 more
05-04 18:58:32.788: E/AndroidRuntime(17215): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.common.GooglePlayServicesUtil.A(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.maps.internal.u.I(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.maps.internal.u.H(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.maps.MapFragment$b.jz(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.dynamic.a.a(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.app.Activity.onCreateView(Activity.java:4242)
05-04 18:58:32.788: E/AndroidRuntime(17215):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
05-04 18:58:32.788: E/AndroidRuntime(17215):    ... 20 more
05-04 18:58:45.413: I/Process(17215): Sending signal. PID: 17215 SIG: 9

【问题讨论】:

【参考方案1】:

你真的很接近,看起来你只是在&lt;activity&gt;下的错误位置添加了&lt;meta-data&gt;标签而不是&lt;application&gt;

尝试使用此代码代替您的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmaphmida"
    android:versionCode="1"
    android:versionName="1.0" >

<permission
    android:name="com.example.androidmaphmida.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.androidmaphmida.permission.MAPS_RECEIVE" />

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name="com.example.androidmaphmida.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
  </activity>

  <!-- Goolge API Key -->
  <meta-data
      android:name="com.google.android.maps.v2.API_KEY"
      android:value="AIzaSyBg-SzhfkXjGUDK037BbmsG4Hv3yrY6c2M" />
  <meta-data
      android:name="android.support.PARENT_ACTIVITY"
      android:value="com.example.androidmaphmida.MainActivity" />
  <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />

</application>

【讨论】:

【参考方案2】:

尝试以这种方式替换您的 Java 文件和布局文件:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity" >

    <TextView
        android:layout_
        android:layout_
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <fragment
        android:id="@+id/map"
        android:layout_
        android:layout_
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity 

 @Override
 protected void onCreate(Bundle savedInstanceState) 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 


【讨论】:

以上是关于谷歌地图如何与安卓系统协同工作的主要内容,如果未能解决你的问题,请参考以下文章

华为手机无法高更新到安卓12

为啥手机上不能用谷歌地图

谷歌Pixel 2搭载安卓8.1系统 10月4日首发

即使安卓设备离线,谷歌地图如何获取地址

创建行程谷歌地图安卓

无法加载安卓谷歌地图 V2