Rory McCrossan
Rory McCrossan

Reputation: 337560

'Error inflating class fragment' error when following Maps API tutorial

I've been following the Xamarin Maps API tutorial and am consistantly running into problems. The most consistent of which is 'Android.Views.InflateException'. I have followed the toturial to the absolute letter, yet cannot find what the issue is. I have also reviewed the other similar questions on this problem, but nothing has helped - the project will not build at all.

MainActivity.cs:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace NewMapApp
{
    [Activity (Label = "NewMapApp", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

                    // this line causes the error
            SetContentView (Resource.Layout.Main);
        }
    }
}

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

Stack trace:

Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895
  at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:3830
  at NewMapApp.MainActivity.OnCreate (Android.OS.Bundle) [0x0000e] in /Users/rorymccrossan/Documents/Projects/Source/Testing/NewMapApp/NewMapApp/MainActivity.cs:26
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:1943
  at at (wrapper dynamic-method) object.10990ff1-ed31-42fe-a0e5-3d5d23292191 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
  at 
  at --- End of managed exception stack trace ---
  at android.view.InflateException: Binary XML file line #1: Error inflating class fragment
  at    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
  at    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
  at    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
  at    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
  at    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
  at    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
  at    at android.app.Activity.setContentView(Activity.java:1956)
  at    at newmapapp.MainActivity.n_onCreate(Native Method)
  at    at newmapapp.MainActivity.onCreate(MainActivity.java:28)
  at    at android.app.Activity.performCreate(Activity.java:5372)
  at    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
  at    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
  at    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
  at    at android.app.ActivityThread.access$700(ActivityThread.java:159)
  at    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
  at    at android.os.Handler.dispatchMessage(Handler.java:99)
  at    at android.os.Looper.loop(Looper.java:137)
  at    at android.app.ActivityThread.main(ActivityThread.java:5419)
  at    at java.lang.reflect.Method.invokeNative(Native Method)
  at    at java.lang.reflect.Method.invoke(Method.java:525)
  at    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
  at    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
  at    at dalvik.system.NativeStart.main(Native Method)
  at Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
  at    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
  at    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
  at    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
  at    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
  at    ... 22 more
  at 

I am attempting to debug on a device (Samsung Galaxy S4) although it's not even getting that far when building.

As said above, I have followed all steps of the tutorial; added Google Play Services, all required permissions and now getting this. Can anyone guide me as to what the problem is?

Upvotes: 0

Views: 4825

Answers (3)

da Rocha Pires
da Rocha Pires

Reputation: 2492

Setting the AndroidManifest.xml file with the following permissions worked for me:

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

Upvotes: 0

Brett Duncavage
Brett Duncavage

Reputation: 2183

It looks like you are not subclassing FragmentActivity, try:

// if you need to be compatible
using Android.Support.V4.App;
// if you don't need to be compatible
using Android.App;
public class MainActivity : FragmentActivity // <- You MUST be a FragmentActivity if you want to use fragments in your activity.

And you should no longer have that runtime issue.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

I found the issue. The tutorial is missing an important setting in the AndroidManifest. As well as requiring certain permissions, you also need to provide the Google Play Services version as a meta-data node:

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

I have notified Xamarin of the problem by email, but will leave this question active in the hopes that it will help someone in the future.

Upvotes: 6

Related Questions