Java Android:膨胀类片段时出错

Java Android:膨胀类片段时出错,java,android,android-layout,android-fragments,google-maps-android-api-2,Java,Android,Android Layout,Android Fragments,Google Maps Android Api 2,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

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:paddingBottom="@dimen/activity_vertical_margin"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    tools:context="com.gaurav.googlemap.HomeMap" >

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

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

</RelativeLayout>
在我搜索这个问题之前,我也做了以下事情来解决

1.导入以下类

import android.support.v4.app.Fragment;  
import android.support.v4.app.FragmentActivity;
2.扩展的
FragmentActivity
class

我仍然在logcat中得到这个错误

Error inflating class fragment

还有别的办法解决这个问题吗?谢谢。

鉴于您使用了“support fragment”,您需要将
MapFragment
替换为
SupportMapFragment
。这样做应该可以解决问题

使用:

而不是:

android:name="com.google.android.gms.maps.MapFragment"
因此,您的xml必须如下所示:

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


你能解释class=“com.google.android.gms.maps.SupportMapFragment”和android:name=“com.google.android.gms.maps.MapFragment”之间的区别吗?我不明白如果事先不知道你的片段类,你打算怎么做?例如,如果您有一个为
片段
保留空间的布局,但是您在该空间中显示的特定片段根据当前应用程序状态而变化。那么它将如何建立呢?
android:name="com.google.android.gms.maps.MapFragment"
<fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>