在填充片段Google Maps API时出错

时间:2016-06-21 07:01:09

标签: java android google-maps android-fragments

我正在关注developers.google来创建地图活动,但是当我运行该应用时,特定活动会因fragment中的setContentView充气错误而崩溃。我搜索了几个解决方案,并在Manifest中提供了playservices版本并使用了SupportMapFragment,但它们都没有工作。

activity_add_place_map.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.petrichors.eguide.AddPlaceMap">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

AddPlaceMap.java:

public class AddPlaceMap extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_place_map);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney, Australia, 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));
}
}

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.petrichors.eguide" >
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-permission android:name="android.permission.INTERNET" />

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

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

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_api_key"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >
..................
..................
</application>

摇篮:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.petrichors.eguide"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
    compile 'com.android.support:design:23.1.1'
    compile 'com.veinhorn.scrollgalleryview:library:1.0.5'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.github.traex.rippleeffect:library:1.3'
    compile 'com.github.florent37:materialtextfield:1.0.5'
    compile 'com.google.android.gms:play-services-maps:7.8.0'
    compile 'com.google.android.gms:play-services-location:7.8.0'
    compile 'com.github.markushi:circlebutton:1.1'
}

错误日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.petrichors.eguide/com.petrichors.eguide.AddPlaceMap}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
            at android.app.ActivityThread.access$800(ActivityThread.java:166)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5584)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331)
            at android.app.Activity.setContentView(Activity.java:2018)
            at com.petrichors.eguide.AddPlaceMap.onCreate(AddPlaceMap.java:19)
            at android.app.Activity.performCreate(Activity.java:5442)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
            at android.app.ActivityThread.access$800(ActivityThread.java:166)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5584)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
            at maps.z.k.a(Unknown Source)
            at maps.x.c.a(Unknown Source)
            at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
            at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(Unknown Source)
            at uw.onTransact(:com.google.android.gms.DynamiteModulesB:62)
            at android.os.Binder.transact(Binder.java:361)
            at qht.newMapFragmentDelegate(SourceFile:179)
            at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(SourceFile:101)
            at qhs.onTransact(SourceFile:62)
            at android.os.Binder.transact(Binder.java:361)
            at com.google.android.gms.maps.internal.zzc$zza$zza.zzs(Unknown Source)
            at com.google.android.gms.maps.SupportMapFragment$zzb.zzzW(Unknown Source)
            at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
            at com.google.android.gms.dynamic.zza.zza(Unknown Source)
            at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
            at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
            at android.support.v4.app.Fragment.onInflate(Fragment.java:1142)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2287)
            at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
            at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
            at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331)
            at android.app.Activity.setContentView(Activity.java:2018)
            at com.petrichors.eguide.AddPlaceMap.onCreate(AddPlaceMap.java:19)
            at android.app.Activity.performCreate(Activity.java:5442)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
            at android.app.ActivityThread.access$800(ActivityThread.java:166)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5584)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

2 个答案:

答案 0 :(得分:4)

如错误所示,<meta-data>必须位于<application>元素内。所以,移动这些行:

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

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_api_key"/>

下的

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme" >

答案 1 :(得分:0)

您缺少API密钥。

 Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

EDIT @antonio发现了原因。