简体   繁体   中英

Android : Error inflating class fragment

XML layout file

<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 file

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);
}
}

As I searched this question before also, I have done following things to solve

1.Imported following classes

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

2.Extended FragmentActivity class

Still I am getting this error in logcat

Error inflating class fragment

Is there any other way to solve this? Thanks.

Given that you have used 'support fragment' you need to replace MapFragment with SupportMapFragment . Doing this should fix it.

Use:

class="com.google.android.gms.maps.SupportMapFragment"

instead of :

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

so your xml must be like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM