1

I am trying to integrate google maps to Xamarin Android. However, received the error, as written in the title. This error appeared on my SetContentView (Resource.Layout.Main); as shown below where:

Error occurred in MainActivity.cs

My google play services for maps is up-to-date. I have tried cleaning and rebuilding the project, also restarting VS did not work for me. Non of the forums answered my questions.

These are what is written in my project for:

MainActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Gms.Maps;

namespace Google_maps.Droid
{
    [Activity (Label = "@string/Google_maps.Android", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity, IOnMapReadyCallback
    {
        private GoogleMap mMap;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            SetUpMap();

        }

        private void SetUpMap()
        {
            if (mMap == null)
            {
                FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
            }
        }

        public void OnMapReady(GoogleMap googleMap)
        {
            mMap = googleMap;
        }

    }
}

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"
    android:name="com.google.android.gms.maps.MapFragment"/>



</LinearLayout>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Click Me!</string>
    <string name="app_name">Google_maps.Android</string>
  <string name="google_maps_key">my_API_key</string>
  <string name="Google_maps.Android">Google maps</string>
</resources>

and finally,

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Google_maps.google_mapsAndroid" android:installLocation="preferExternal">
  <uses-sdk android:minSdkVersion="15" />
  <application android:label="Google_maps.Android" android:icon="@drawable/Icon">
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    <meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="@string/google_maps_key" />
  </application>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
</manifest>

For additional information, I followed a method from a youtuber, Joe Rock, where he teaches us on how to setup google maps. Here is the link to his video:

https://www.youtube.com/watch?v=W6Q0olRPsus

If you look down the comment section, you can see many others are having the same problem too. This video was made 2 years ago, so I would understand why it would not work at this current situation. However, I would appreciate it if you guys can enlighten me for the proper way to integrate google maps to my android mobile application, if somehow, this method is no longer reliable. Thank you in advance.

regards, Xamarin and programming newbie.

Edit:

When I drag my mouse to SetContentView (Resource.layout.Main) it says void Activity.SetContentView(int LayoutResID) (+2 overloads) set the activity content on a layout resource.

Should I be concern about this?

Bah Akif
  • 33
  • 5
  • Generally, Fragments cannot be embedded inside a class which extends activity, instead it should extent FragmentActivity – Dinash Oct 11 '17 at 05:01
  • Then may I know what exactly I should do? – Bah Akif Oct 11 '17 at 06:23
  • Change `public class MainActivity : Activity ` to `public class MainActivity : FragmentActivity` and add appropriate import statement – Dinash Oct 11 '17 at 06:26
  • @Dinash done, I've imported `using Android.Support.V4.App`, and another method `Android.Support.V4.App.FragmentActivity, IOnMapReadyCallBack`. Received the same error as before; ( Unhandled Exception: Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment occurred) – Bah Akif Oct 11 '17 at 06:42
  • In what API are you trying to run your code? Also share build gradle. – HaroldSer Oct 11 '17 at 06:57
  • Google maps Android API. Sadly, there is no gradle in Xamarin.Android. @ScottS Although, from the Nuget Packager, I'm using a binding called Xamarin.GooglePlayServices.Maps (v.42.10.21.1) – Bah Akif Oct 11 '17 at 07:10
  • I mean in what android version? – HaroldSer Oct 11 '17 at 07:12
  • oh, Android 7.1 (Nougat). – Bah Akif Oct 11 '17 at 07:16

3 Answers3

1

So my project couldn't open, in a case where I had to re-create my project, from scratch. I've made changes on my emulator, which I think might've been the problem all this time, and managed to get it to work and removed the error on the title.

Although, the current and only problem is, that it only displays the "google" logo on the bottom left, but no map. I should probably write this in another thread? Since its a different case now.

But so far, the error got removed, which I believe, the devices and android version, plays a big role in this.

EDIT:

Hello again, I've managed to get google maps fully working on my app. As I said before, "the devices and android version, plays a big role in developing applications". FYI, I'm using Android version 7.1 (Nougat), Nexus 5 device, Google API x86 - API 25.

  1. Set compiler to 8.0 (Oreo) in properties >> Application and target to "Use Compile Using SDK Version" in properties >> Android Manifest.

  2. Update your GoogleMaps Package by Xamarin Inc. from NuGet Package Manager, found in Tools to the latest stable version. My current latest stable version is 42.1021.1.

  3. Update your Xamarin.Android.Support.Compat by Xamarin Inc. from Nuget Package Manager, found in tools to the latest stable version. My current latest stable version is 26.0.2. If fragment library is not updated along with this, just update manually.

  4. I added possibly missing permissions as shown below:

<permission android:name="googleMapsProject.googleMapsProject.droid.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
 <uses-permission android:name="com.deg.blubcnmobl.droid.permission.MAPS_RECEIVE" />

FYI The googleMapsProject.googleMapsProject is your package name. Make sure it doesn't start with an Upper case and matches with the package name you write in the Key Restriction page in google console.

  1. All this is running on v2 of google maps API.

That's pretty much how I got it working. Hope this helps!

Bah Akif
  • 33
  • 5
0

Try:

Change:

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

To:

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

Also, in your SetUpMap() method change below:

FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);

for:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
HaroldSer
  • 2,025
  • 2
  • 12
  • 23
0

1.Install Appcompact dll from Nuget

2.Use AppCompatActivity instead of Activity

3.Add the following permissions in your AndroidManifest under your properties

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

4.Add the Following xml in your main.axml

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

5.Add the following code in your styles.xml under resource/values/styles :

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#FFCC01</item>
<item name="colorPrimaryDark">#E69D02</item>
<item name="colorControlHighlight">#FFCC01</item>
<item name="colorAccent">#D5012A</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

 <style name="DrawerArrowStyle" 
  parent="@style/Widget.AppCompat.DrawerArrowToggle">
  <item name="spinBars">true</item>
  <item name="color">@color/colorRedText</item>
 </style>

<style name="MyTheme" parent="MyTheme.Base">
 </style>

6.Make my theme as your default application theme by adding the following lines in your AndroidManifest.xml file which you can find under Properties of your project,below the current android project.

  <application android:label="YourAppName" android:theme="@style/MyTheme" android:hardwareAccelerated="true" android:largeHeap="true" android:icon="@drawable/YourAppIcon">

And in case you dont understand anything you could simply check here :

https://developer.xamarin.com/guides/android/platform_features/maps_and_location/

Also check if you have Google play services in your AndroidSdk which you can find in the toolbar in VS-15 tools>Android>AndroidSdk Install Google Play Services under Extras.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • Thank you @G.hakim for responding. I've tried this method, however throws in a new error on `SetContentView (Resource.layout.Main)` error: **Unhandled Exception: Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. occurred**. Also when I drag my mouse to `SetContentView (Resource.layout.Main)` it says **void Activity.SetContentView(int LayoutResID) (+2 overloads) set the activity content on a layout resource.** Should I be concern about this? – Bah Akif Oct 12 '17 at 00:21
  • I have updated my answer take a look and no its an error because you do not have a default theme once you update as per above code it shall vanish @BahAkif – FreakyAli Oct 12 '17 at 04:56
  • I think we're almost there. I'm going through one error, which appeared in the **styles.xml**, showing an error; **No resource found that matches the given name (at 'drawerArrowStyle' with value '@style/DrawerArrowStyle')**. I'm using a component called **Android Support Library v7 AppCompat** and installed a nugget package called **Xamarin.Android.Support.v7.AppCompat** both by **Xamarin.inc**. Am I downloading the right package and component? – Bah Akif Oct 12 '17 at 07:57
  • Yes Actually i didnt include one style sorry you can check the updated code above & you have installed the right nuget package @BahAkif – FreakyAli Oct 12 '17 at 09:29
  • It gave the same error for `@color/colorRedText`, just like the previous for DrawerArrowStyle. I've tried removing `@color/colorRedText`, and still creates the same `unhandled exception` error at `SetContentView(Resource.layout.main)`, which pretty much, goes back to the same case. I think, the error came from somewhere in my axml. – Bah Akif Oct 16 '17 at 00:18
  • remove that @color/colorRedText and put a hex code of some color there – FreakyAli Oct 16 '17 at 07:10