0

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

希望有人可以帮助我吗?

这是我的片段的源代码,显示了 MapView 公共类 FragmentAdvertlistMap extends 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();
}


}
4

1 回答 1

0

尝试将其添加到 onCreateView()

container.removeAllViews();

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

于 2014-03-04T04:53:44.367 回答