2

我的英文不好。我想在ViewPager中使用MapFragment,但是我的代碼不工作。請幫幫我。ViewPager中的Android MapFragment不工作

我的應用程序使用白色背景顏色,當MapFragment開始時,背景顏色變成灰色,以及全部。

我fragment_map.xml

<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" /> 

我MapFragment.java

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(); 
} 

} 

我的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="com.arshad.map.permission.MAPS_RECEIVE" /> 

<permission 
    android:name="com.arshad.map.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<application 
    android:name=".Application" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity android:name=".activities.MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".activities.TweetActivity" /> 
    <activity android:name=".activities.LoginActivity" /> 

    <meta-data 
     android:name="io.fabric.ApiKey" 
     android:value="is not null, i deleted this" /> 
    <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="is not null, i deleted this" /> 

</application> 

</manifest> 

回答

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); 
} 
相關問題