Home > Net >  when my xamarin form app run, it's has stopped?
when my xamarin form app run, it's has stopped?

Time:07-04

I create a blank app in xamarin form for Android. when i run blank app in emulator thas work exactly, but when add Xamarin.Form.GoogleMaps to my page and run in emulator show error!
error message = "Application has stopped"
I did the following to add Xamarin.Form.GoogleMaps .

1- download latest version Xamarin.Form.GoogleMaps from Nuget.


2- Add <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="my api key" /> to AndroidManifest.xml

3- add all permission i need! list of permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

4- add xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps" to my page

5- and add <maps:Map VerticalOptions="Fill" HorizontalOptions="Fill"/>

Friends, how can I solve this problem of closing the app?

CodePudding user response:

You are missing the plugin initialization in the Android project.

using Xamarin.Forms.GoogleMaps.Android;
namespace App.Droid
{
   public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
       protected override void OnCreate(Bundle savedInstanceState)
       {
            // more stuff here...
            var platformConfig = new PlatformConfig
            {
               BitmapDescriptorFactory = new CachingNativeBitmapDescriptorFactory()
            };
            Xamarin.FormsGoogleMaps.Init(this, savedInstanceState, 
            platformConfig); // initialize for Xamarin.Forms.GoogleMaps
            }
            LoadApplication(new App());
        }
    }
}

CodePudding user response:

At first, you can download the android studio and check the log in the emulator to find what type of error appears and causes the app crash.

In addition, there is already a sample in the github for this nuget package, have you tried that?

Finally, at the 4th step, you not only need to add these permissions into the AndroidManifest.xml, but also need the user to grant the dangerous permissions such as the location permission by himself.

So you can do a run-time permission check and request the permission according to the official document:https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows and https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android

  • Related