我刚开始编写 Android 应用程序,在尝试将 Google map 实现到 fragment 中时出现此错误。我在网上冲浪,发现这是一个非常普遍的问题,但我也看到所有其他解决方案都不适用于我的情况。我将所有导入的代码部分粘贴到下面:
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 Build Path”窗口中的“Android private libraries”是否已正确检查,如您所见,我已经导入了“com.google.android.gms.maps.SupportMapFragment;”我还导入了“google-play-services.jar”库。 我的错误在下面一行:
GoogleMap map = ((MapSupportFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
如果您需要其他部分的代码,请询问,我会粘贴 :)
P.S.:我的 IDE 是 Eclipse。
最佳答案
尝试以下步骤,也许你错过了一些:
- 将 google play 服务库复制到您的 lib 文件夹中
- 将 android 支持的 v4 库复制到您的 lib 文件夹中
- 将 google play 服务库和 android 支持的 v4 添加到您的构建路径
- 将 google play 服务项目导入到您的工作区中选择您的 项目并右键单击>属性>android>添加项目库( google play 服务)并且不要勾选“是图书馆”
- 选择 google play 服务项目并右键单击并转到 属性>android>勾选“是lib”
- 使用 FragmentActivity 并使用 Support 来使用支持的库。
- 清理并重建,退出 eclipse,卸载 以前在您的设备上的应用程序,加载 Eclipse 并执行另一个清理和 重建。
关于java - 实现 Google Maps Android 时出错 - SupportMapFragment 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25928681/