暂停后,标签中的地图会冻结。恢复

时间:2013-06-19 12:09:24

标签: android

我正在使用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

编辑:我刚刚发现了类似的情况here。这可能就是答案,但我不知道从建议的解决方案中放置代码的位置。

编辑:以防关键代码不在我的代码段中,这就是整件事......

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

}

3 个答案:

答案 0 :(得分:4)

这个添加的行容器.removeAllViewsInLayout()似乎已经完成了这个伎俩。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{   
    container.removeAllViewsInLayout();

    View view = inflater.inflate(R.layout.mapview, container, false);

答案 1 :(得分:0)

您是否在mMapView.displayZoomControls(true)之后尝试使用mMapView = (MapView) view.findViewById(R.id.mapv)

答案 2 :(得分:0)

我也有同样的问题 我做过类似的事情

将保留片段的片段活动

Map_main_screen.java

public class Map_main_screen extends FragmentActivity {
    private static final String[] CONTENT = new String[] { "TAB one",
            "Second tab GOOGLE MAP", };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
                setContentView(R.layout.map_new);
        FragmentPagerAdapter adapter = new GoogleMusicAdapter(
                getSupportFragmentManager());

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);

==========================new_class_GoogleMusicAdapter============================
    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Map_google_TestFragment.newInstance(CONTENT[position
                    % CONTENT.length]);

        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }
    // =================adapter ends here===============================
}

Map_google_TestFragment.java

  public final class Map_google_TestFragment extends Fragment {
        private static final String KEY_CONTENT = "TestFragment:Content";
        Typeface tf;

        public static Map_google_TestFragment newInstance(String content) {
    Map_google_TestFragment fragment = new  Map_google_TestFragment();

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 1; i++) {
                builder.append(content).append(" ");
            }
            builder.deleteCharAt(builder.length() - 1);
            fragment.mContent = builder.toString();

            return fragment;
        }


        private String mContent = "???";
        private int[] mICONS = { 1 };

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if ((savedInstanceState != null)
                    && savedInstanceState.containsKey(KEY_CONTENT)) {
                mContent = savedInstanceState.getString(KEY_CONTENT);
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            View v;
            if (mContent.toString().equals("TAB one")) {
                v = vi.inflate(R.layout.YOUR_SECOND_LAYOUT, null);

                          //***************************************
                          // function related to your first fragment 
                          //***************************************

            } else {

                          //***************************************
                          // inflate your Google  map layout here 
                          // function related to your Second fragment 
                          //***************************************
                    View view = inflater.inflate(R.layout.mapview, container, false); //INFLATE GOOGLE MAP LAYOUT
                     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 onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString(KEY_CONTENT, mContent);
        }

    }