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

时间:2013-12-04 17:17:08

标签: android google-maps xamarin

我一直关注着Xamarin Maps API tutorial,并且一直遇到问题。其中最一致的是'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);
        }
    }
}

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>

堆栈追踪:

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 

我正在尝试在设备(三星Galaxy S4)上进行调试,尽管在构建时它甚至没有那么远。

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

3 个答案:

答案 0 :(得分:6)

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

<application>
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>

我已通过电子邮件通知Xamarin这个问题,但会将此问题保持活跃,希望将来可以帮助某人。

答案 1 :(得分:0)

看起来您不是FragmentActivity的子类,请尝试:

// if you need to be compatible
using Android.Support.V4.App;
// if you don't need to be compatible
using Android.App;
public class MainActivity : FragmentActivity // <- You MUST be a FragmentActivity if you want to use fragments in your activity.

您应该不再遇到运行时问题。

答案 2 :(得分:0)

设置具有以下权限的AndroidManifest.xml文件对我有用:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />