如何解决;发生了未处理的异常;使用谷歌地图Android应用程序-沙马林

如何解决;发生了未处理的异常;使用谷歌地图Android应用程序-沙马林,android,google-maps,xamarin,xamarin.android,Android,Google Maps,Xamarin,Xamarin.android,我制作了一个运行谷歌地图的应用程序,然后根据你的输入找到你想要的地方。每次我通过模拟器运行它时,它都会弹出消息“发生了未处理的异常”。我不知道如何解决这个问题,如果这是模拟器的问题,或者我的代码有什么问题 这是我的主要活动: using Android.App; using Android.Widget; using Android.OS; using Android.Gms.Maps; using Android.Views; using Android.Gms.Maps.Model; usi

我制作了一个运行谷歌地图的应用程序,然后根据你的输入找到你想要的地方。每次我通过模拟器运行它时,它都会弹出消息“发生了未处理的异常”。我不知道如何解决这个问题,如果这是模拟器的问题,或者我的代码有什么问题

这是我的主要活动:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Gms.Maps;
using Android.Views;
using Android.Gms.Maps.Model;
using System.Collections.Generic;
using Android.Locations;
using System.Linq;
using Android.Util;
using System.Threading.Tasks;

namespace SafeandSound
{
[Activity(Label = "SafeandSound", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    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);
        }
    }

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

    }
    // Button to Search for Address//
    public async System.Threading.Tasks.Task onMapSearchAsync(View view)
    {
        EditText gotAddress = (EditText)FindViewById(Resource.Id.searchText);
        string addressnew = gotAddress.Text;
        if (!Geocoder.IsPresent)
        {
            Log.Error("SO", "Geocoder is not present");
        }
        else
        {
            var geocoder = new Geocoder(this);
            var retry = 0;
            do
            {
                var addressList = await geocoder.GetFromLocationNameAsync(addressnew, 5);
                if (addressList.Count > 0)
                {
                    foreach (var address in addressList)
                    {
                        Log.Debug("SO", $"{address.Latitude}:{address.Longitude} - {address.FeatureName} : {address.GetAddressLine(0)} : {address.GetAddressLine(1)}");
                        LatLng latLng = new LatLng(address.Latitude, address.Longitude);
                        CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
                        builder.Target(latLng);
                        builder.Zoom(10);
                        CameraPosition cameraPosition = builder.Build();
                        CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);

                    }
                    break;
                }
                retry++;
                Log.Warn("SO", $"No addresses returned...., retrying in {retry * 2} secs");
                await Task.Delay(retry * 1000);
            } while (retry < 5);
        }

    }
}
}
使用Android.App;
使用Android.Widget;
使用Android.OS;
使用Android.Gms.Maps;
使用Android.Views;
使用Android.Gms.Maps.Model;
使用System.Collections.Generic;
使用Android.Locations;
使用System.Linq;
使用Android.Util;
使用System.Threading.Tasks;
名称空间安全和声音
{
[活动(Label=“SafeandSound”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私有谷歌地图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
SetUpMap();
}
私有void SetUpMap()
{
如果(mMap==null)
{
FragmentManager.FindFragmentById(Resource.Id.map);
}
}
4月1日公开作废(谷歌地图谷歌地图)
{
mMap=谷歌地图;
}
//按钮搜索地址//
公共异步System.Threading.Tasks.Task onMapSearchAsync(视图)
{
EditText gotAddress=(EditText)FindViewById(Resource.Id.searchText);
string addressnew=gotAddress.Text;
如果(!Geocoder.IsPresent)
{
日志错误(“SO”,“地理编码器不存在”);
}
其他的
{
var geocoder=新的地理编码器(本);
var重试=0;
做
{
var addressList=wait geocoder.GetFromLocationNameAsync(addressnew,5);
如果(addressList.Count>0)
{
foreach(地址列表中的var地址)
{
Log.Debug(“SO“,$”{address.Latitude}:{address.Longitude}-{address.FeatureName}:{address.GetAddressLine(0)}:{address.GetAddressLine(1)}”);
LatLng LatLng=新LatLng(地址.纬度,地址.经度);
CameraPosition.Builder=CameraPosition.InvokeBuilder();
建造商目标(latLng);
放大(10);
CameraPosition CameraPosition=builder.Build();
CameraUpdate CameraUpdate=CameraUpdateFactory.NewCameraPosition(cameraPosition);
}
打破
}
重试++;
Log.Warn(“SO“,$”没有返回地址…,在{retry*2}秒内重试”);
等待任务。延迟(重试*1000);
}而(重试次数<5次);
}
}
}
}
这是我的主要代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/searchText"
        android:layout_width="303.5dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:hint="Search Location..."
        android:textColor="#000000" />
    <Button
        android:id="@+id/search_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:onClick="onMapSearch"
        android:text="Search" />
</LinearLayout>
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"/>
 </LinearLayout>

注意:这假设您从一个基于模板的Xamarin.Android项目开始

添加谷歌地图: 将
Xamarin.GooglePlayServices.Maps
包添加到项目中

Main.axml 将默认的
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="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_weight="0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:layout_weight="10"
            android:id="@+id/searchText"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="Search Location..."/>
        <Button
            android:layout_weight="1"
            android:id="@+id/search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search" />
    </LinearLayout>
    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
</LinearLayout>
AndroidManifest.xml 获得密钥后,将其添加到项目中。您可以添加程序集级别的
元数据
属性,而不是手动修改
AndroidManifest.xml

将以下内容添加到
属性/AssemblyInfo.cs

[assembly: MetaData("com.google.android.geo.API_KEY", Value = "YourGoogleMapApiKeyGoesHere")]
搜索“星巴克98101”的结果:

将您的
Resource.Layout.Main
axml添加到您的问题中。@sushingover我以前尝试过添加它,但它不允许我添加,现在我修复了它,您得到的
完全异常是什么,因为我看到了许多可能出现问题的事情……我有API密钥,但看起来我的主要活动是一团糟的!你的代码工作正常谢谢!你能解释一下我的错在哪里吗?只是想了解这整件事。有几件事,但我没有逐行去做,重要的是你没有将
IOnMapReadyCallback
添加到你的activity类中,因此map ready方法永远不会被调用,因此你的google map变量永远不会被赋值……好的,谢谢你的输入,刚开始学这门语言,只是在努力学习!再次感谢你的帮助
http://console.developers.google.com/
[assembly: MetaData("com.google.android.geo.API_KEY", Value = "YourGoogleMapApiKeyGoesHere")]