3

這裏是我的活動代碼java.lang.ClassCastException:android.support.v4.app.NoSaveStateFrameLayout不能轉換到com.google.android.maps.MapView

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解決方案,但仍然無法解決我的問題,請給我一些解決方案。 謝謝

回答

6

我無法理解你的代碼:

  1. 你是第一個試圖獲得MapView

    mapView = (MapView) findViewById(R.id.map); 
    
  2. 然後你試圖讓SupportMapFragment

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

從相同的用戶界面元素:R.id.map

雖然MapView是谷歌地圖API V1元素和SupportMapFragment是從API V2

你在R.layout.map佈局中使用什麼?告訴我們它是代碼。

,如果你想使用API​​ V2,這裏博客文章中,我寫了一篇關於如何在地圖應用程序中的集成:

Google Maps API V2

+0

'雖然MapView的是谷歌地圖API V1'錯的部分'Google Map API V2' – 2014-06-17 06:08:38

相關問題