我正在开发一个应用程序,顶部有一个菜单,该菜单中有一些按钮。我正在尝试将Google地图添加到其中一个按钮,但我不是很成功。我正在关注this tutorial,但我没有实现谷歌地图直接导入MainActivity
,而是将它们添加到按下按钮时启动的片段中。一切正常。当我按下地图加载按钮时,他们就像他们应该那样工作。当我按下home键返回到MainActivity
它完美的作品,但是当我要加载地图再次它给了我debugging error: Class File Editor: Source not found
安卓谷歌地图在片段
这是GoogleMaps
片段代码:
public class GoogleMaps extends Fragment{
private GoogleMap googleMap;
double latitude = 46.514249;
double longitude = 15.080183;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View myFragmentView = inflater.inflate(R.layout.maps_layout, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
return myFragmentView;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
任何帮助将不胜感激。编辑: 如果我不够具体。它在我第一次加载fragment
时有效,但当我尝试再次加载时(第二次)它不起作用。
编辑v2: 我实际上找到解决方案我自己。 我所要做的就是添加OnDestroyView
方法:
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
还是要谢谢你。
我ANM面临同样的问题,我没有加上'onDestroyView()'方法,但它并没有帮助..如果你能帮我 –
如果你不解释你的问题,如果我没有看到代码,我不能帮你... – Izak
谢谢Izak,但我已经解决了。:) –