我正在尝试在片段中显示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 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"?>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.test.mobile.activity.LoginActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.test.mobile.activity.MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my_apikey_value"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
答案 0 :(得分:0)
我认为问题出在您的drawMap
:
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
,onDestroy
,onResume
等)传递给MapView
。见this section of docs
我唯一要提到的是,由于你在片段内膨胀MapView
,你需要在MapView
内拨打onCreate
onCreateView()
:
@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();
.....
}
等等