0

我正在尝试将 Google Map V2 集成到我在 android studio 中的项目中。我在 build.gradle 文件中添加了 google play 服务作为依赖项,如下所示:

dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:21.0.0'
        compile 'com.google.android.gms:play-services:6.1.71'
    }

我还更改了我的AndroidManifest.xml文件,如下所示:

<permission  android:name="app.me.locationalarm.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="app.me.locationalarm.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <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"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY_API_KEY" />
</manifest>

在我的视图布局中,我添加了一个片段。

  <fragment
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

在我的活动中,我创建了一个方法createMapView,如下所示:

private void createMapView(){
        try {
            if(null == googleMap){
                     googleMap =  ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
            if(null == googleMap) {
                    Toast.makeText(getApplicationContext(),
                            "Error creating map", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (NullPointerException exception){
            Log.e("mapApp", exception.toString());
        }
    }

我从我的 OnCreate 方法中调用了这个方法。当我尝试执行我的项目时,我发现了这样的错误:

FATAL EXCEPTION: main
    Process: app.me.locationalarm, PID: 18446
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.me.locationalarm/app.me.locationalarm.HomeActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

我的目标 SdkVersion 是 21。知道如何解决吗?我找到了一些答案,但这些都没有帮助我。

4

2 回答 2

2

将“元数据”标签信息放在 mainfest 的“应用程序”标签中。

<?xml version="1.0" encoding="utf-8"?>

<permission
    android:name="app.me.locationalarm.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="app.me.locationalarm.permission.MAPS_RECEIVE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<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"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HomeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="API_KEY" />
</application>

于 2014-11-25T22:06:07.263 回答
1

尝试用 SupportMapFragment 替换 MapFragment。它会是这样的:

 <fragment
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

googleMap =  ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.mapView)).getMap();
于 2014-11-20T06:22:34.227 回答