2014-09-19 46 views
1

我在編程android應用程序的開始,當我嘗試將Google地圖實現爲片段時出現此錯誤。我瀏覽了網頁,發現這是一個非常常見的問題,但我也看到所有其他解決方案都不適用於我的案例。我貼的代碼以下的部分與全部進口:實施谷歌地圖時出錯Android - SupportMapFragment無法解析爲類型

public class GeolocalizzazioneFragment extends Fragment { 
private final LatLng CENTER_POINT = new LatLng(44.22625, 12.06861); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_geolocalizzazione, container, false); 
     //super.onCreateView(savedInstanceState); 
     //setContentView(R.layout.activity_main); 
     //ottieni il controllo del fragment su cui caricare la mappa 
     GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
     //centra la mappa su CENTER_POINT, con zoom 5 
     //map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTER_POINT, 5)); 
     //centra la mappa sulla posizione del device 
     map.setMyLocationEnabled(true); 
     //animazione dello zoom sulla nostra animazione all'apertura 
     //Parametri: livello di zoom=10; durata animazione = 1000 millisecondi (1 sec) 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 1000, null); 

     try{ 
      URL url = new URL("### url where I get xml with location marker datas ###"); //prendo l'URL del file XML dal quale prendo i dati dei POI 
      URLConnection conn = url.openConnection();   //instauro la connessione col file XML 

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder builder = factory.newDocumentBuilder(); 
      org.w3c.dom.Document doc = builder.parse(conn.getInputStream()); 

      NodeList nodes = doc.getElementsByTagName("marker"); 
      for (int i=0; i<nodes.getLength(); i++) 
      { 
       Element item = (Element) nodes.item(i); 
       String name = item.getAttribute("name"); 
       String address = item.getAttribute("address"); 
       String stringLat = item.getAttribute("lat"); 
       String stringLong = item.getAttribute("long"); 
       String icon = item.getAttribute("icon"); //assigned variable for the XML icon attribute 
       Double lat = Double.valueOf(stringLat); 
       Double lon = Double.valueOf(stringLong); 

       // map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
       // map.setMyLocationEnabled(true); 

       map.addMarker(new MarkerOptions() 
           .position(new LatLng(lat,lon)) 
           .title(name) 
           .snippet(address) 
       ); 
      } 
     }catch (Exception e){ 
      e.printStackTrace(); 

     } 


     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

    } 

我也查了,在「Java構建路徑」窗口「Android的私人圖書館」正確檢查,你可以看到我已經導入「com.google .android.gms.maps.SupportMapFragment;」並且我還導入了「google-play-services.jar」庫。 我的錯誤是在下面的一行:

GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

如果你需要的代碼的其他部分,只問我會貼:)

P.S:我的IDE就是Eclipse。

+0

此錯誤* GoogleMap map =((MapSupportFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap(); *? – Gattsu 2014-09-19 07:42:22

+0

就是這條線.. @maven – 2014-09-19 07:44:08

+0

也許你錯過了com.google.android.gms.maps.SupportMapFragment的導入語句。 – Gattsu 2014-09-19 07:45:58

回答

1

嘗試以下步驟,也許你錯過了一些:

  • 複製谷歌播放服務lib放到您的lib文件夾
  • 複製Android的支持V4的lib在你的lib文件夾
  • 加入谷歌播放服務lib和Android支持v4到您的構建路徑
  • 將google play服務項目導入到您的工作區中選擇您的 項目並右鍵單擊>屬性> android>添加項目庫( google play服務)並執行不勾選「是圖書館」
  • 選擇谷歌播放服務項目,右鍵單擊並轉到 屬性>機器人>嘀「的LIB」
  • 使用FragmentActivity並使用支持使用支持庫。
  • 清理和重建,退出eclipse,卸載 應用程序以前在您的設備上,加載日食和做另一個乾淨和 重建。
+0

我完全搞砸了最後一個!感謝您指出! – shyam 2014-09-19 07:59:41

+0

@shyam沒有問題謝謝你的幫助:) – 2014-09-19 08:04:31

+0

@JoaoBiriba我會按照你的步驟,我會寫你的結果,非常感謝你一步一步的指導:D – 2014-09-19 08:05:21