suraj kumar
suraj kumar

Reputation: 66

How to resolve error inflating class fragment

I am trying to use google map in my application. To achieve that I have downloaded and added google play services as a library to my application, also I have got the map key from google console.

But I am getting this error in the logcat -

   android.view.InflateException: Binary XML file line #23: Error inflating class fragment

My apps manifest file is as follows -

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

  <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission                                       android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />                 
  <application
    android:allowBackup="true"
    android:icon="@drawable/pin_location"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.surajkumar.myapp.MainActivity"
        android:label="@string/app_tag" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/gmap_apiv2_key" />
  </application>

  </manifest>

The layout file is -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@color/honeydew" 
>
<TextView 
    android:id="@+id/yourlocationtxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Refresh to get your location"/>
<LinearLayout 
android:id="@+id/mapsection"
android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="4dp">

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

</LinearLayout>
<Button
    android:id="@+id/sendsms"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/sendlocationbtntxt" 
    android:background="@drawable/button_style"/>

</LinearLayout>

The Activity code is -

package com.surajkumar.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


 public class MainActivity extends Activity {

private GoogleMap peoplesLocMap;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    peoplesLocMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();    
  }

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

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);

}


  }

I am getting the error at line

  setContentView(R.layout.activity_main);

the logcat displays the following -

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #23: Error inflating class fragment
E/AndroidRuntime(1221):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
E/AndroidRuntime(1221):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
E/AndroidRuntime(1221):     at android.app.Activity.setContentView(Activity.java:1895)
E/AndroidRuntime(1221):     at com.surajkumar.myapp.MainActivity.showLocMapView(MainActivity.java:146)
E/AndroidRuntime(1221):     at com.surajkumar.myapp.MainActivity.access$0(MainActivity.java:145)
E/AndroidRuntime(1221):     at com.surajkumar.myapp.MainActivity$2.onClick(MainActivity.java:88)
E/AndroidRuntime(1221):     at android.view.View.performClick(View.java:4240)
E/AndroidRuntime(1221):     at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime(1221):     at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(1221):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1221):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1221):     at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(1221):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1221):     at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(1221):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1221): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
E/AndroidRuntime(1221):     at android.app.Fragment.instantiate(Fragment.java:585)
E/AndroidRuntime(1221):     at android.app.Fragment.instantiate(Fragment.java:560)
E/AndroidRuntime(1221):     at android.app.Activity.onCreateView(Activity.java:4738)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
E/AndroidRuntime(1221):     ... 21 more

Can any one please help. I tried many of the suggested solutions on stackoverflow but none seemed to work. Please help me to get away with this issue. Thanks in advance.

Upvotes: 0

Views: 435

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Change this

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

to

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

You can change min sdk to 12 and above in manifest considering this https://developer.android.com/about/dashboards/index.html?utm_source=ausdroid.net

If you want map in api level 11 and below use SupportMapFragment extend FragmentActivity.

If its above 12 use MapFragment and extend Activity.

You are also missing a meta-tag

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

You are also missing

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 1

Related Questions