Android View InflateException错误

时间:2016-03-28 18:30:23

标签: android google-maps android-fragments

所以,android.view.InflateException我遇到了很多麻烦,我不能完全确定原因。当我尝试加载Google Maps Fragment时会发生这种情况。

非常感谢任何有关此错误的想法/帮助。

03-28 19:11:50.061 17870-17870/com.example.beng09715.restaurant_finder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.beng09715.restaurant_finder, PID: 17870
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beng09715.restaurant_finder/com.example.beng09715.restaurant_finder.Cocktails}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
                                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:198)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6837)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                                      Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
                                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                                                                                         at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450)
                                                                                         at android.app.Activity.setContentView(Activity.java:2321)
                                                                                         at com.example.beng09715.restaurant_finder.Cocktails.onCreate(Cocktails.java:19)
                                                                                         at android.app.Activity.performCreate(Activity.java:6500)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
                                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6837) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                                                                                      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.db.r.a(Unknown Source)
                                                                                         at maps.cz.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 com.google.android.gms.maps.internal.d$a.onTransact(:com.google.android.gms.alldynamite:62)
                                                                                         at android.os.Binder.transact(Binder.java:395)
                                                                                         at com.google.android.gms.maps.internal.ah.b(:com.google.android.gms:179)
                                                                                         at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms:100)
                                                                                         at com.google.android.gms.maps.internal.ag.onTransact(:com.google.android.gms:62)
                                                                                         at android.os.Binder.transact(Binder.java:395)
                                                                                         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:357)
                                                                                         at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                                         at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
                                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
                                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                                                                                         at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450) 
                                                                                         at android.app.Activity.setContentView(Activity.java:2321) 
                                                                                         at com.example.beng09715.restaurant_finder.Cocktails.onCreate(Cocktails.java:19) 
                                                                                         at android.app.Activity.performCreate(Activity.java:6500) 
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072) 
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
                                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6837) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

的Manifest.xml

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

<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permision.READ_GSERVICES" />
<uses-library android:name="com.google.android.maps" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.maps.V2.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Home_screen"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity
        android:name=".Breakfast"
        android:label="@string/title_activity_breakfast"
        android:parentActivityName=".Home_screen"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.beng09715.restaurant_finder.Home_screen" />

        <intent-filter>
            <action android:name="com.example.beng09715.restaurant_finder.Breakfast" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu"
        android:label="@string/app_name"></activity>
    <activity
        android:name=".Lunch"
        android:label="@string/app_name"></activity>
    <activity
        android:name=".AfternoonTea"
        android:label="@string/app_name"></activity>
    <activity
        android:name=".Dinner"
        android:label="@string/app_name"></activity>
    <activity
        android:name=".Cocktails"
        android:label="@string/title_activity_cocktails">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.beng09715.restaurant_finder.Home_screen" />
    </activity>

    <!--
         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/. 
    -->

    <activity
        android:name=".MapViewer"
        android:label="@string/title_activity_map_viewer">
    </activity>
</application>

MapViewer.java

package com.example.beng09715.restaurant_finder;

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 MapViewer extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_viewer);
    // 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));
}
}

Cocktails.xml

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

1 个答案:

答案 0 :(得分:2)

您的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

首先,更改清单中API密钥引用的新格式。

改变这个:

    <meta-data
    android:name="com.google.android.maps.V2.API_KEY"
    android:value="@string/google_maps_key" />

对此:

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

请注意,如果您还没有这样做,则需要使用keytool来获取调试密钥库和发布密钥库的SHA1指纹,并在Developer Console上设置API密钥

有关从调试/发布密钥库中获取SHA1指纹的信息,see here

您将在开发者控制台的API密钥中使用这些SHA1指纹:

enter image description here

设置好API密钥后,将其放入"@string/google_maps_key",即可完成设置。