我在編程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。
此錯誤* GoogleMap map =((MapSupportFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap(); *? – Gattsu 2014-09-19 07:42:22
就是這條線.. @maven – 2014-09-19 07:44:08
也許你錯過了com.google.android.gms.maps.SupportMapFragment的導入語句。 – Gattsu 2014-09-19 07:45:58