ViewPager中的Android MapFragment无法正常工作

时间:2015-10-02 16:58:42

标签: android android-fragments android-viewpager android-maps-v2

我的英语不好。我想在ViewPager中使用MapFragment,但我的代码无效。求你帮帮我。

我的应用程序使用白色背景颜色,当MapFragment启动时,背景颜色变为灰色,所有。

我的fragment_map.xml

app\build\outputs\mapping\release

我的MapFragment.java

<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginBottom="?attr/actionBarSize" />

我的AndroidManifest.xml

package com.lomovskiy.android.testapp.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.lomovskiy.android.testapp.R;
import com.lomovskiy.android.testapp.activities.MainActivity;

public class MapFragment extends Fragment implements OnMapReadyCallback {

private MapView mMapView = null;

private GoogleMap mGoogleMap = null;

public static MapFragment newInstance() {
    Bundle args = new Bundle();
    MapFragment fragment = new MapFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MapsInitializer.initialize(MapFragment.this.getActivity().getBaseContext());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.fragment_map, container, false);
    mMapView = (MapView) v.findViewById(R.id.map_view);
    mMapView.onCreate(savedInstanceState);
    mMapView.getMapAsync(this);
    return v;
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    mGoogleMap.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker")
            .draggable(true)
    );
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mMapView.onSaveInstanceState(outState);
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

}

1 个答案:

答案 0 :(得分:0)

您的onCreateView()方法应该像现在一样拥有视图通胀代码,但是您应该将地图相关代码移动到onViewCreated(),这是在Fragment实际完全构建时运行的。

可能使用当前代码,mMapView = (MapView) v.findViewById(R.id.map_view)实际上可能会返回null。

所以你可以尝试类似的东西:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  super.onCreateView(inflater, container, savedInstanceState);
  View v = inflater.inflate(R.layout.fragment_map, container, false);
  return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  mMapView = (MapView) v.findViewById(R.id.map_view);
  mMapView.onCreate(savedInstanceState);
  mMapView.getMapAsync(this);
}