【问题标题】:Android google map v2安卓谷歌地图v2
【发布时间】:2013-04-28 12:13:26
【问题描述】:

您好,我使用了谷歌地图 v2,如果我运行应用程序

public class SocMap extends Activity {

}

打开地图但如果我运行应用程序

public class SocMap extends FragmentActivity {

}

应用崩溃

错误:

04-28 15:10:03.405: E/AndroidRuntime(12041): FATAL EXCEPTION: main
04-28 15:10:03.405: E/AndroidRuntime(12041): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.socmap/com.example.socmap.SocMap}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
04-28 15:10:03.405: E/AndroidRuntime(12041): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
04-28 15:10:03.405: E/AndroidRuntime(12041): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment

【问题讨论】:

    标签: android google-maps map google-maps-api-2


    【解决方案1】:

    如果您想在您的应用程序中实现 Google 地图,并且您希望您的应用程序以 API FragmentActivity。 FragmentActivitygoogle-support-v4 库的一部分,基本上支持 API 级别 11 之前的系统片段。在这种情况下,您必须导入此库。您可以通过阅读我在 Google 地图集成上写的这篇博文的第 4 步来了解它是如何完成的:

    Google Maps API V2

    您只需要记住,如果您采用这种方式,您应该为您的地图使用 SupportMapFragment 对象,而不是您当前使用的 MapFragment

    另一方面,如果您将应用开发到 API 级别 11 或更高级别,则无需执行此步骤,您只需使用简单的 ActivityMapFragment

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      在您的布局中,您看起来像是在使用 MapFragment。请改用android:name="com.google.android.gms.maps.SupportMapFragment",并在您的onCreate 中,像这样获取MapFragment:

      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment); //your SupportMapFragment's id will need to be set to this in your layout.
      

      【讨论】:

        【解决方案4】:

        试试这个项目,您将获得有关 Google Map v2 的所有答案。

        Android Working with Google Map V2

        【讨论】:

          【解决方案5】:

          您的错误跟踪显示ClassCastException

          java.lang.ClassCastException:com.google.android.gms.maps.MapFragment 无法转换为 android.support.v4.app.Fragment

          这意味着您使用的 API 小于 11 和 FragmentActivityMapFragment。尝试使用 ActivityMapFragment 更新 API > 11。

          如果您想继续使用 API SupportMapFragment 而不是 MapFragment

          参考这里使用 API 实现 MapFragment > 11 Google Map v2

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-25
            • 1970-01-01
            • 2013-04-30
            • 1970-01-01
            相关资源
            最近更新 更多