java.lang.ClassCastException:android.support.v4.app.NoSaveStateFrameLayout无法强制转换为com.google.android.maps.MapView

时间:2013-03-22 06:30:47

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

这是我的活动代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    mapView = (MapView) findViewById(R.id.map);

GoogleMap map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map))
        .getMap();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.id.map, container, false);
}

这是我的LogCat

03-22 11:53:38.497: E/AndroidRuntime(436): FATAL EXCEPTION: main
03-22 11:53:38.497: E/AndroidRuntime(436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alltaskass/com.example.alltaskass.Map}: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.maps.MapView
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.os.Looper.loop(Looper.java:126)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.ActivityThread.main(ActivityThread.java:3997)
03-22 11:53:38.497: E/AndroidRuntime(436):  at java.lang.reflect.Method.invokeNative(Native Method)
03-22 11:53:38.497: E/AndroidRuntime(436):  at java.lang.reflect.Method.invoke(Method.java:491)
03-22 11:53:38.497: E/AndroidRuntime(436):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-22 11:53:38.497: E/AndroidRuntime(436):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-22 11:53:38.497: E/AndroidRuntime(436):  at dalvik.system.NativeStart.main(Native Method)
03-22 11:53:38.497: E/AndroidRuntime(436): Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.maps.MapView
03-22 11:53:38.497: E/AndroidRuntime(436):  at com.example.alltaskass.Map.onCreate(Map.java:35)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-22 11:53:38.497: E/AndroidRuntime(436):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
03-22 11:53:38.497: E/AndroidRuntime(436):  ... 11 more

我已经访问了thisthis解决方案,但我仍然无法解决问题,请给我一些解决方案。 谢谢

1 个答案:

答案 0 :(得分:6)

我无法理解你的代码:

  1. 您首先尝试获得MapView

    mapView = (MapView) findViewById(R.id.map);
    
  2. 然后你试图获得SupportMapFragment

    GoogleMap map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    
  3. 来自同一个UI元素:R.id.map

    虽然MapView是Google地图API V1元素而SupportMapFragment来自API V2

    您在R.layout.map布局中使用了什么?告诉我们它的代码。

    如果你想使用API​​ V2,我写了一篇关于如何在你的应用程序中集成地图的博客文章:

    Google Maps API V2