2014-01-05 96 views
0

我花了最近几个小时试图找出我做错了什么。我正在尝试使用适用于Android v2的Google地图制作基本地图应用,但我收到此错误。错误使用谷歌地图膨胀课堂片段

01-05 15:34:10.878: E/AndroidRuntime(22687): FATAL EXCEPTION: main 
01-05 15:34:10.878: E/AndroidRuntime(22687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapapp/com.example.mapapp.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.ActivityThread.access$600(ActivityThread.java:151) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.os.Looper.loop(Looper.java:155) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.ActivityThread.main(ActivityThread.java:5536) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at java.lang.reflect.Method.invokeNative(Native Method) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at java.lang.reflect.Method.invoke(Method.java:511) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at dalvik.system.NativeStart.main(Native Method) 
01-05 15:34:10.878: E/AndroidRuntime(22687): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:363) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.Activity.setContentView(Activity.java:1912) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.example.mapapp.MainActivity.onCreate(MainActivity.java:14) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.Activity.performCreate(Activity.java:5066) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
01-05 15:34:10.878: E/AndroidRuntime(22687): ... 11 more 
01-05 15:34:10.878: E/AndroidRuntime(22687): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. 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" /> 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.maps.internal.q.v(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.maps.internal.q.u(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.maps.SupportMapFragment$b.cE(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.dynamic.a.a(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.dynamic.a.onInflate(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290) 
01-05 15:34:10.878: E/AndroidRuntime(22687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 

我不知道是怎么回事错误的,但这里是我的MainActivity.java

package com.example.mapapp; 


import android.os.Bundle; 
import android.view.Menu; 
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); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

这里是我的activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    Copyright (C) 2012 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 
<!-- This can go anywhere in your layout (see other demos for some examples). --> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" 
     android:name="com.testing.svma.MainActivity"/> 

这里是我的AndroidManifest.xml

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

     <!-- Copied from Google Maps Library/AndroidManifest.xml. --> 
    <uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="19"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <!-- External storage for caching. --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <!-- My Location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <!-- Maps API needs OpenGL ES 2.0. --> 
    <uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/> 
    <!-- End of copy. --> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
    <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="CENSORED" /> 
     <activity 
      android:name="com.example.mapapp.MainActivity" 
      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> 

</manifest> 

任何帮助将非常感谢!

+0

[Google Play服务更新至版本13后出现错误]的可能重复(http://stackoverflow.com/questions/19723811/after-google-play-service-update-to-version-13-i -got-an-error) – laalto

回答

0

classandroid:name属性具有相同的功能。您fragment标签更改为以下:

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

这样做android:name="com.example.mapapp.MainActivity"你告诉Android的使用MainActivity就好像它是一个Fragment

此外,您的AndroidManifest.xml未正确配置Google Maps V2。这meta标签具体为:

<meta-data 
      android:name="com.google.android.gms.version" 
      android:value="AIzaSyAl0-Oe2uYMSt40LFqxt5VMy0lXXX" /> 

请参阅谷歌docs关于如何正确配置谷歌地图V2。另外,请确保您正确导入Google地图库。

作为建议,不要将您的API密钥与世界分享。

+0

嗯好吧谢谢。我用你的代码替换了我的代码,但是我仍然收到“Error inflate class fragment: –

+0

@Grantprice请看我编辑的答案。 – Emmanuel

+0

好吧我检查了我的API密钥,如果你能在你的答案中做同样的事情会很好。改变了标签 '<元数据 机器人:名字=“com.google.android.maps.v2.API_KEY” 机器人:值=“API_KEY” />' ,但仍然得到同样的错误我是错过了什么?谢谢你 –

相关问题