我正在使用 actionbarsherlock 来实现一组选项卡。其中一个选项卡是包含 MapView 的片段。我的 onCreateView 如下:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.mapview, container, false);
mMapView = (MapView) view.findViewById(R.id.mapv);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PARIS, 10));
googleMap.addMarker(new MarkerOptions().position(PARIS).title("Paris").snippet("Great city"));
return view;
}
如您所见,地图最初是在巴黎放大显示的,上面有一个标记。到目前为止一切正常。我看到了地图,可以手动拖动和缩放地图。如果我然后按下手机侧面的按钮使其暂停,然后解锁手机以恢复,我仍然可以拖动和缩放。但是,如果我切换到另一个选项卡,然后返回到地图选项卡,我会在离开时看到地图,但它不再响应拖动/缩放。它似乎冻结了。但是我的日志中没有显示任何错误,我仍然可以在选项卡之间切换。
没有任何日志错误,我不知道如何追踪问题。
编辑:我正在使用地图v2
编辑:我刚刚在这里发现了类似的情况。这很可能是答案,但我不知道将建议的解决方案中的代码放在哪里。
编辑:以防万一关键代码不在我的代码段中,这就是全部...
package com.mycompany.myapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
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.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.view.View;
public class TestMapFragment extends Fragment implements TabListener
{
private Fragment mFragment;
private MapView mMapView;
private GoogleMap googleMap;
private static final LatLng PARIS = new LatLng(48.874,2.347);
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.mapview, container, false);
mMapView = (MapView) view.findViewById(R.id.mapv);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PARIS, 10));
googleMap.addMarker(new MarkerOptions().position(PARIS).title("Paris").snippet("Great city"));
return view;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
mFragment = new TestMapFragment();
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
/*
* Using a mapview in a fragment requires you to 'route'
* the lifecycle events of the fragment to the mapview
*/
@Override
public void onResume()
{
super.onResume();
if (mMapView != null)
{
mMapView.onResume();
}
}
@Override
public void onPause()
{
super.onPause();
if (mMapView != null)
{
mMapView.onPause();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
if (mMapView != null)
{
mMapView.onDestroy();
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
if (mMapView != null)
{
mMapView.onSaveInstanceState(outState);
}
}
@Override
public void onLowMemory()
{
super.onLowMemory();
if (mMapView != null)
{
mMapView.onLowMemory();
}
}
}