谷歌地图未显示在标签片段中



我在项目中使用选项卡布局。其中一个选项卡片段应显示世界地图。我的应用程序与谷歌地图API集成,我使用ViewPager来设置适配器。 以下是活动。

public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), HomeActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Cloud", "Home", "Report" };
Context context;
public PagerAdapter(FragmentManager fm, HomeActivity context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MapFragment();
case 1:
return new OneFragment();
case 2:
return new TwoFragment();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(HomeActivity.this).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
}
}

我的片段是

public class MapFragment extends Fragment  {
private MapView mMapView;
private GoogleMap mMap;
View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_map, null, false);
mMapView = (MapView) view.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(
getActivity(),
"Lat : " + latLng.latitude + " , "
+ "Long : " + latLng.longitude,
Toast.LENGTH_SHORT).show();
}
});
if (mMap == null) {
mMapView = (MapView) view.findViewById(R.id.mapView);
mMapView.onCreate(getArguments());
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity());
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap gmap) {
mMap = gmap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//fill markers
//add marker listener
}
});

} catch (Exception e) {
e.printStackTrace();
}
}
}
});
return view;
}
@Override
public void onResume(){
super.onResume();
mMapView.onResume();
}
@Override
public void onPause(){
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy(){
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory(){
super.onLowMemory();
mMapView.onLowMemory();
}

}

我的应用程序成功运行。 在第一个选项卡中,可以加载片段,并且在单击屏幕时也可以看到纬度和经度值。 但看不到谷歌地图。在图片下方共享。相同的代码在单独的活动中完美运行。请帮助找到我错过添加的内容。 截图

我已经通过为 Google 地图 API 生成一个新密钥并在清单文件中更新它来解决此问题。现在,我能够在我的一个选项卡片段中看到谷歌地图,与其他片段完美配合。

最新更新