Reputation: 33
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?
Upvotes: 1
Views: 885
Reputation: 33
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.
Set compiler to 8.0 (Oreo) in properties >> Application and target to "Use Compile Using SDK Version" in properties >> Android Manifest.
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.
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.
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.
That's pretty much how I got it working. Hope this helps!
Upvotes: 1
Reputation: 16459
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.
Upvotes: 0
Reputation: 2065
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);
Upvotes: 0