【问题标题】:Android : Error inflating class fragmentAndroid:膨胀类片段时出错
【发布时间】:2015-04-15 23:50:36
【问题描述】:

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>

Java 文件

package com.beproject.ourway;

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

public class HomeMap extends FragmentActivity {

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

因为我之前也搜索过这个问题,我已经做了以下事情来解决

1.导入以下类

import android.support.v4.app.Fragment;  
import android.support.v4.app.FragmentActivity;

2.扩展FragmentActivity

我仍然在 logcat 中收到此错误

Error inflating class fragment

还有其他方法可以解决这个问题吗?谢谢。

【问题讨论】:

    标签: java android android-layout android-fragments google-maps-android-api-2


    【解决方案1】:

    鉴于您使用了“支持片段”,您需要将MapFragment 替换为SupportMapFragment。这样做应该可以解决它。

    用途:

    class="com.google.android.gms.maps.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" 的区别吗?
    • 我不明白在您事先不知道您的片段类的情况下您打算做什么?例如,如果您有一个为Fragment 保留空间的布局,但您在该空间中显示的特定片段会因当前应用程序状态而异。那么它是如何设置的呢?
    猜你喜欢
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2011-09-19
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多