在android xamarian上添加map的错误是什么?

时间:2015-05-18 18:35:31

标签: android google-maps xamarin xamarin-studio

我正在尝试将谷歌地图v2添加到xamarian中的Android应用程序中,有时会崩溃,它强制关闭,或者在实施Google Maps Android API v2时获得Null GoogleMap / Crashing,这是我的代码:

我的XML文件

DECLARE @yourTable TABLE (names VARCHAR(15));
INSERT INTO @yourTable VALUES('hello]'),('[hello]]'),('"hello]"');

SELECT  names AS QuotenameVal,
        CASE
            WHEN LEFT(names,1) = '"'
                 AND RIGHT(names,1) = '"'
                THEN SUBSTRING(names,2,LEN(names) - 2)
            WHEN LEFT(names,1) = '['
                 AND RIGHT(names,1) = ']'
                THEN SUBSTRING(names,2,LEN(names) - 2)
            ELSE names
        END Actual_Value
FROM @yourTable

我的班级

QuotenameVal    Actual_Value
--------------- ---------------
hello]          hello]
[hello]]        hello]
"hello]"        hello]

那是我的AndroidManiFeast

<?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"
    android:id="@+id/mainlayout">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

然后崩溃,并且他不能在这个类中使用setcontentview

并且我正在尝试使用其他方式添加地图,但它会强制接近使用此代码的应用

我的XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Gms.Maps.Model;
using Android.Gms.Maps;
using Android.Support.V4.Graphics;
using Android.Support.V7.AppCompat;

namespace CyclingAndroid
{
    [Activity (Label = "MapActivity")]          
    public class MapActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            var mapFragment = new MapFragment ();
            FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
            fragmentTx.Add (Resource.Id.mainlayout, mapFragment);
            fragmentTx.Commit ();
            SetContentView (Resource.Layout.Map_View);
        }

        protected override void OnResume ()
        {
            base.OnResume ();


        }
    }
}

我的班级

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="GooglePlayServicesApp.GooglePlayServicesApp">
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" />
    <application android:label="GooglePlayServicesApp">
        <!-- Put your Google Maps V2 API Key here. This key will not work for you.-->
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="yourKey" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    <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" />
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:glEsVersion="0x00020000" android:required="true" />
</manifest>

它将是相同的Androidmainifeast

我的错误是 “不幸的是,此应用程序在几秒钟后停止工作

这就是我在logcat中获得的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="vertical"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="fill_parent"
    p1:layout_height="fill_parent"
    p1:id="@+id/linearLayout121">
    <TextView
        p1:text="Simple google maps test"
        p1:textAppearance="?android:attr/textAppearanceSmall"
        p1:id="@+id/textView121"
        p1:layout_width="fill_parent"
        p1:layout_height="wrap_content" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

对于第一种方法(在xml布局中声明地图片段),您无需创建新的MapFragment并通过FragmentTransaction添加它。这已经通过布局完成了。要获取MapFragment并在其中实例化GoogleMap,您应该执行以下操作:

(在OnCreate中,在SetContentView之后)

((SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.MapFragmentIdName)).GetMapAsync(this);

这假定您正在使用SupportFragments,如果不是,则只需更改为常规FragmentManager。然后,您应该在活动中实施IOnMapReadyCallback,以便在GoogleMap对象准备好后对其进行操作。

希望有所帮助!