Android 安卓:地图不显示
我试图在片段中显示一个地图视图。地图视图从不显示。我错过了什么Android 安卓:地图不显示,android,google-maps,android-mapview,Android,Google Maps,Android Mapview,我试图在片段中显示一个地图视图。地图视图从不显示。我错过了什么 public class FriendsGroupMapFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; View frie
public class FriendsGroupMapFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
View friendsGroupMapFragmentView;
GoogleMap friendsMap;
private OnFragmentInteractionListener mListener;
public FriendsGroupMapFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("ON CREATEVIEW()","ON CREATEVIEW()");
friendsGroupMapFragmentView = inflater.inflate(R.layout.fragment_friends_group_map, container, false);
drawMap();
return friendsGroupMapFragmentView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onResume() {
super.onResume();
drawMap();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
//public void onFragmentInteraction(Uri uri);
}
public void drawMap(){
Log.d("drawMap","drawMap");
if(friendsMap != null){
MapsInitializer.initialize(getActivity());
friendsMap = ((MapView)friendsGroupMapFragmentView.findViewById(R.id.mapfriends)).getMap();
if(friendsMap == null){
Log.d("NULL","NULL");
}
LatLng chennai = new LatLng(13.0839,80.2700);
friendsMap.moveCamera(CameraUpdateFactory.newLatLng(chennai));
}
}
}
我的xml文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.mobile.activity.FriendsGroupMapFragment">
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapfriends"/>
</FrameLayout>
我的舱单:
<?xml version="1.0" encoding="utf-8"?>
我想问题出在你的
绘图图上
:
public void drawMap(){
Log.d("drawMap","drawMap");
if(friendsMap != null){
MapsInitializer.initialize(getActivity());
friendsMap = ((MapView)friendsGroupMapFragmentView.findViewById(R.id.mapfriends)).getMap();
if(friendsMap == null){
Log.d("NULL","NULL");
}
LatLng chennai = new LatLng(13.0839,80.2700);
friendsMap.moveCamera(CameraUpdateFactory.newLatLng(chennai));
}
}
仅当已创建地图时,才在此处创建地图:)
试着换成
if(friendsMap == null)
{
....
}
编辑
我完全没有注意到您使用了MapView
而不是MapFragment
使用MapView
时,需要将所有片段生命周期事件(onCreate
、onestroy
、onResume
等)传递到MapView
。看
我要提到的唯一一件事是,由于在片段内部膨胀了MapView
,因此需要调用MapView
的onCreate
内部onCreateView()
:
等等你能发布你的xml和清单吗?@giacomoni补充道。请注意。谢谢不知道您是否发布了没有api密钥的代码,但请确保您有api密钥而不是我的值。是的。我有api密钥。我只是没有在这里加上。我改了,现在我得到了NPE。getMap()为Null也是将mapfragment放在我的片段中而不是mapview的更好方法吗?这是使
mapview
工作的唯一方法-手动调用生命周期事件:)。实际上,文件上说你应该这样做。有了MapFragment
,它就更容易了——你不必担心生命周期——它为你做了一切。因此,选择哪种方式取决于你。嵌套片段的问题是它们是在API 16中引入的,因此如果您想在另一个片段中使用MapFragment,并且您的min SDK是可以的。。谢谢:)我将MapView更改为SupportMapFragment:)
if(friendsMap == null)
{
....
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("ON CREATEVIEW()","ON CREATEVIEW()");
friendsGroupMapFragmentView = inflater.inflate(R.layout.fragment_friends_group_map, container, false);
this.mapView = (MapView)friendsGroupMapFragmentView.findViewById(R.id.mapfriends);
this.mapView.onCreate(savedInstanceState);
drawMap();
return friendsGroupMapFragmentView;
}
@Override
public View onResume() {
this.mapView.onResume();
.....
}