Android:MapView在Resume之后没有响应

时间:2013-08-13 19:42:56

标签: android background android-mapview

com.google.android.gms.maps.MapView出现了一个奇怪的问题。 为了检查我的应用程序是否在垃圾收集器完成其工作后崩溃,我强制我的HTC One(4.2.2)只允许在后台运行1个应用程序。如果我在显示MapView时离开我的应用程序(主页按钮),启动任何其他应用程序并恢复到我的应用程序,我的MapView仍然显示...但我无法移动或缩放地图,它根本没有响应。其他活动工作正常。我真的不知道问题可能在哪里。

希望有人可以帮助我吗?

这是我的片段的源代码,显示了MapView     公共类FragmentAdvertlistMap扩展了Fragment {

com.google.android.gms.maps.MapView m;
GoogleMap mMap;
ArrayList<Advert> ads;
HashMap<Marker, String> myMarker;
public final LatLngBounds.Builder builder= new LatLngBounds.Builder();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
     try {
        MapsInitializer.initialize(getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO handle this situation
    }
    View inflatedView = inflater.inflate(R.layout.activity_advert_tab2, container, false);
    m = (com.google.android.gms.maps.MapView)inflatedView.findViewById(R.id.map_tab);
    m.onCreate(savedInstanceState);

    myMarker = new HashMap<Marker, String>();
    ads= AdvertListActivity.getAdverts();
    setUpMapIfNeeded(inflatedView);
    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            Intent myIntent = new Intent(getActivity(), AdvertLocationActivity.class);

            Advert putadvert = DefaultApplication.dbc.getAdvertForAdvertID(Integer.parseInt(myMarker.get(arg0)));

            myIntent.putExtra("advert", putadvert);
            startActivity(myIntent);

        }
    });
    return inflatedView;
}


private void setUpMapIfNeeded(View inflatedView) {
    if (mMap == null) {
        mMap = ((com.google.android.gms.maps.MapView) inflatedView.findViewById(R.id.map_tab)).getMap();
        if (mMap != null) {
            this.initMarker();
        }
    }
}

public void initMarker(){
    for(int i=0;i<ads.size();i++){
        Advert tempAd = ads.get(i);
        LatLng tlalo =  new LatLng(tempAd.mainLocation.latitude,tempAd.mainLocation.longitude);

        builder.include(tlalo);

        String address = "";
        if(tempAd.mainLocation.contact_street != null){
            address = address + tempAd.mainLocation.contact_street;
        }
        if(tempAd.mainLocation.contact_street_number != null){
            address = address + " " + tempAd.mainLocation.contact_street_number;
        }
        Marker marker = mMap.addMarker(new MarkerOptions()
        .position(tlalo)
        .anchor(0.5f, 0.5f)
        .title(tempAd.name)
        .snippet(address)
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.androidpin)));

        myMarker.put(marker,String.valueOf(tempAd.myid));
    }



mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition arg0) {
        mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
        mMap.setOnCameraChangeListener(null);
    }

});
}


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

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

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

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


}

1 个答案:

答案 0 :(得分:0)

尝试在onCreateView()

中添加它
container.removeAllViews();

我在另一个类似的问题中找到了它,但我丢失了答案的原始链接......