【问题标题】:Android Google Map API v2 - Error Inflating Class Fragment android.view.InflateExceptionAndroid Google Map API v2 - 错误膨胀类片段 android.view.InflateException
【发布时间】:2015-02-02 10:50:02
【问题描述】:

我正在关注 Google Maps Android API v2 文档以在我的应用程序中使用地图。我收到错误“”我已经尝试了所有方法,但仍然无法解决。

这是我的 MainActivity.java

package com.example.maps;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

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

这是 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"
    tools:context="${relativePackage}.${activityClass}" >

  <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

</RelativeLayout>

这是我在 logcat 中遇到的错误

02-02 16:02:46.110: E/AndroidRuntime(11094): FATAL EXCEPTION: main
02-02 16:02:46.110: E/AndroidRuntime(11094): Process: com.example.maps, PID: 11094
02-02 16:02:46.110: E/AndroidRuntime(11094): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maps/com.example.maps.MainActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class fragment

Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />
    <permission 
        android:name="com.example.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"
        ></permission>
    <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"/>
    <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature 
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/gyan_logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name="com.example.maps.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>
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBJ4S3tVGq4GWjgsuJ2zm0EAfUPN3yFKKA"/>
    </application>

</manifest>

请帮我解决这个错误。

【问题讨论】:

  • 使用 android:name="com.google.android.gms.maps.SupportMapFragment"
  • 也发布你的manifest.xml 。这里主要播放。

标签: java android xml google-maps android-fragments


【解决方案1】:

你应该改变

  android:name="com.google.android.gms.maps.MapFragment"

  android:name="com.google.android.gms.maps.SupportMapFragment"

因为你使用的是FragmentActivity

编辑:

您还需要添加您的manifest.xml

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

【讨论】:

  • 我将它从 Activity 更改为 FragmentActivity,同时尝试实现发布的类似问题之一的答案。我尝试实施您的解决方案,但仍然显示相同的错误。请帮忙。
  • 我已经在 manifest.xml 中添加了 跨度>
  • @Peaceful_Warrior 在哪里??我看不见。你必须有两个&lt;meta-data&gt; 标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多