2013-04-28 219 views
0

嗨,我使用谷歌地图V2,如果我运行的应用程序Android的谷歌地图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 

回答

1

如果您想在您的应用中实施Google地图并且您希望您的应用程序针对API < 11,那么您将不得不使用FragmentActivityFragmentActivitygoogle-support-v4库的一部分,基本上为API级别11之前的系统提供了片段支持。在这种情况下,您将不得不导入此库。你可以通过阅读这篇博客的第4步得到它是如何做的想法,我在谷歌地图整合写道:

Google Maps API V2

你一定要记住,如果这是你去,你应该使用的方式SupportMapFragment对象,而不是您目前正在使用的MapFragment

如果另一方面你开发你的应用程序到API的11级和更高,这一步是没有必要的,你可以简单地使用ActivityMapFragment

0

在你的布局,它看起来像使用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.