关注Maps API教程时出现"错误导致类片段错误"错误

Ror*_*san 0 android google-maps xamarin

我一直在关注Xamarin Maps API教程,并且一直遇到问题.其中最一致的是'Android.Views.InflateException'.我已经完成了绝对的信件,但却无法找到问题所在.我还回顾了关于这个问题的其他类似问题,但没有任何帮助 - 该项目根本不会构建.

MainActivity.cs:

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

namespace NewMapApp
{
    [Activity (Label = "NewMapApp", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

                    // this line causes the error
            SetContentView (Resource.Layout.Main);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895
  at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:3830
  at NewMapApp.MainActivity.OnCreate (Android.OS.Bundle) [0x0000e] in /Users/rorymccrossan/Documents/Projects/Source/Testing/NewMapApp/NewMapApp/MainActivity.cs:26
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:1943
  at at (wrapper dynamic-method) object.10990ff1-ed31-42fe-a0e5-3d5d23292191 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
  at 
  at --- End of managed exception stack trace ---
  at android.view.InflateException: Binary XML file line #1: Error inflating class fragment
  at    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
  at    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
  at    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
  at    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
  at    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
  at    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
  at    at android.app.Activity.setContentView(Activity.java:1956)
  at    at newmapapp.MainActivity.n_onCreate(Native Method)
  at    at newmapapp.MainActivity.onCreate(MainActivity.java:28)
  at    at android.app.Activity.performCreate(Activity.java:5372)
  at    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
  at    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
  at    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
  at    at android.app.ActivityThread.access$700(ActivityThread.java:159)
  at    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
  at    at android.os.Handler.dispatchMessage(Handler.java:99)
  at    at android.os.Looper.loop(Looper.java:137)
  at    at android.app.ActivityThread.main(ActivityThread.java:5419)
  at    at java.lang.reflect.Method.invokeNative(Native Method)
  at    at java.lang.reflect.Method.invoke(Method.java:525)
  at    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
  at    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
  at    at dalvik.system.NativeStart.main(Native Method)
  at Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
  at    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
  at    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
  at    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
  at    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
  at    ... 22 more
  at 
Run Code Online (Sandbox Code Playgroud)

我试图在一台设备(三星Galaxy S4)上进行调试,虽然它在构建时甚至没有那么远.

如上所述,我已经按照本教程的所有步骤进行了操作; 添加了Google Play服务,所有必需的权限,现在获得此权限.任何人都可以指导我的问题是什么?

Ror*_*san 6

我发现了这个问题.该教程缺少AndroidManifest中的重要设置.除了需要某些权限外,您还需要将Google Play服务版本作为元数据节点提供:

<application>
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
Run Code Online (Sandbox Code Playgroud)

我已经通过电子邮件通知了Xamarin这个问题,但是会让这个问题保持活跃,希望它能帮助将来的某个人.