2

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.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
93gaurav93
  • 99
  • 4
  • 12

1 Answers1

3

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"/>
bpncool
  • 131
  • 1
  • 13
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • Can you explain difference between class="com.google.android.gms.maps.SupportMapFragment" and android:name="com.google.android.gms.maps.MapFragment"? – 93gaurav93 Feb 15 '15 at 10:20
  • http://stackoverflow.com/questions/10162983/activity-layout-fragment-class-vs-androidname-attributes – Shayan Pourvatan Feb 15 '15 at 10:21
  • I don't understand what you're meant to do in the case where your fragment class is not known in advance? For instance, if you've got a layout that reserves space for a `Fragment`, but the particular fragment that you display in that space varies depending upon the current application state. How is it meant to be set up then? – aroth Aug 08 '16 at 07:39