0

I getting this erro when I run my application after adding the Xamarin.Forms.Maps NuGet package. I am trying to display a Map in my MapPage. I have hadded name space definitions to my XAML file.

I have and I have added Xamarin.FormsMaps.Init(this, bundle) to the Android project in the MainActivity and to the iOS project using Xamarin.FormsMaps.Init(); in the AppDelegate file.

I searched google and stackoverflow and couldn't find any useful document.

here is the MainPage.xaml code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TravelRecord"
             x:Class="TravelRecord.MainPage">

    <StackLayout VerticalOptions="Center" Margin="20,0">
        <Entry Placeholder="Enter Your Email... " Keyboard="Email" x:Name="emailEntry" 
               TextColor="{StaticResource blueColor}" Text="Test"/>
        <Entry Placeholder="Enter Your Password... " TextColor="{StaticResource blueColor}"
               IsPassword="True"  x:Name="passwordEntry" Text="Test"/>
        <Button Text="Log in" x:Name="LoginButton" Clicked="LoginButton_Clicked" Margin="0,50,0,0"
                BackgroundColor="{StaticResource blueColor}" TextColor="White"/>
    </StackLayout>

</ContentPage>

and the MainPage.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TravelRecord
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LoginButton_Clicked(object sender, EventArgs e)
        {
           bool emailIsNullOrEmpty =  string.IsNullOrEmpty(emailEntry.Text);
            bool passwordIsNullOrEmpty = string.IsNullOrEmpty(passwordEntry.Text);

            if (emailIsNullOrEmpty || passwordIsNullOrEmpty)
            {

            }

            else
            {

                    Navigation.PushAsync(new HomPage());
            }
        }
    }
}

from which I navigate to the MapsPage using the Login button.

here is the MapsPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelRecord.MapPage"
             xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps">

    <ContentPage.Content>
        <maps:Map />
    </ContentPage.Content>
</ContentPage>

and the MapsPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TravelRecord
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LoginButton_Clicked(object sender, EventArgs e)
        {
           bool emailIsNullOrEmpty =  string.IsNullOrEmpty(emailEntry.Text);
            bool passwordIsNullOrEmpty = string.IsNullOrEmpty(passwordEntry.Text);

            if (emailIsNullOrEmpty || passwordIsNullOrEmpty)
            {

            }

            else
            {

                    Navigation.PushAsync(new HomPage());
            }
        }
    }
}

here MainActivity.cs of the android project:

using System;

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

namespace TravelRecord.Droid
{
    [Activity(Label = "TravelRecord", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            Xamarin.FormsMaps.Init(this, bundle);

            string dataBaseName = "travelRecord.db";
            var dataBaseBath =  System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string fullPath = Path.Combine(dataBaseBath, dataBaseName);


            LoadApplication(new App(fullPath));
        }
    }
}

and the AppDelegate of the iOS project

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Foundation;
using UIKit;

namespace TravelRecord.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            Xamarin.FormsMaps.Init();

            string dataBaseName = "travelRecord.db";
            var dataBaseBath =Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"..","Library");
            string fullPath = Path.Combine(dataBaseBath, dataBaseName);



            LoadApplication(new App(fullPath));

            return base.FinishedLaunching(app, options);
        }
    }
}

I have also added this line to the AndroidManifest.xml file

<meta-data android:name="com.google.android.geo.API_KEY" android:value ="AIzaSyBXuv3VQ4-5pxLcbje-397wvmb3y6RoxsE"></meta-data>

Does anyone know how to solve this ? thanks

Dyary
  • 753
  • 7
  • 14
  • the InnerException property of the Exception will probably have more detail about the root cause of the problem – Jason Sep 02 '18 at 14:06
  • Thanks Jason. For some reason the previous error is gone . now I am getting this : Java.Lang.RuntimeException: API key not found. Check that is in the element of AndroidManifest.xml – Dyary Sep 02 '18 at 16:33

0 Answers0