【问题标题】:SupportMapFragment map disappears on rotationSupportMapFragment 地图在旋转时消失
【发布时间】:2015-06-09 21:48:11
【问题描述】:

我正在使用基于地图的应用程序,该应用程序创建包含地图和 Web 视图的相册视图。该问题发生在轮换期间,并且在 com.android.support:support-v4:19.1 上运行良好,但在我更新到 22.2.0 后无法正常工作。

地图在初始视图中按预期显示,但在旋转后,地图不再出现。我验证了 GoogleMap、容器视图(保存地图)和 SupportMapFragment 都是有效的。

在调试时,我决定检查 SupportMapFragment 的视图,看看这是否可能是原因。旋转后,对 SupportMapFragment 上的 getView() 的调用返回 null (我怀疑这就是地图没有出现的原因,但我不明白为什么它最初有效并且旋转后为 null)。

我不知所措,因为据我所知,我有一个有效的 SupportMapFragment、一个有效的 GoogleMap 和一个具有适当大小(在本例中为 680x800)的容器视图。正如我上面所说,在我使用 19.1 之前的版本中,此代码按预期工作。

下面是相关的sn-ps代码:

SupportMapManager 创建在 EventReportAlbumActivity 类中

public class EventReportAlbumActivity extends FragmentActivity
{
   @Nullable protected SupportMapFragment m_mapFragment;

   @NotNull
   public SupportMapFragment getMapFragment ()
   {
     if( m_mapFragment == null )
     {
        GoogleMapOptions options = new GoogleMapOptions();
        options.compassEnabled( false );
        options.rotateGesturesEnabled( false );
        options.scrollGesturesEnabled( false );
        options.tiltGesturesEnabled( false );
        options.zoomControlsEnabled( false );
        options.zoomGesturesEnabled( false );

        m_mapFragment = SupportMapFragment.newInstance( options );
        m_mapFragment.setRetainInstance( true );
     }
     return m_mapFragment;
  }
}

显示地图和 web 视图的实际 Fragment 实例

public class EventAlbumItemFragment extends Fragment
{
   @Nullable
   protected SupportMapFragment m_mapFragment;

  public static EventAlbumItemFragment newInstance ( )
  {
     final EventAlbumItemFragment fragment = new EventAlbumItemFragment();
     fragment.setRetainInstance( true );

     return fragment;
  }

    @Override
    public View onCreateView ( @NotNull LayoutInflater inflater,
                               @NotNull ViewGroup container,
                               @Nullable Bundle savedInstanceState )
    {
       final View view = inflater.inflate( R.layout.event_album_item, container, false );

       View mapContainer = view.findViewById( R.id.mapview_container );

       // make map fragment view invisible until we configure it so the coordinates will be correct
       mapContainer.setVisibility( View.INVISIBLE );

       // other code removed for brevity...
   }

  /**
   * Setter for the map fragment and its UI
   *
   * @param mapFragment The map fragment which should be added to this fragment's view, or null to remove the current fragment
   */
  public void setMapFragment ( @Nullable SupportMapFragment mapFragment )
  {
     final FragmentManager fragmentManager = getChildFragmentManager();

     if( m_mapFragment != null && m_mapFragment.isAdded() )
     {
        // Remove the existing map manager
        fragmentManager.beginTransaction()
           .remove( m_mapFragment )
           .commit();
     }

     if( mapFragment != null )
     {
        // Add the map view to the current view
        fragmentManager.beginTransaction()
           .add( R.id.mapview_container, mapFragment, MAP_FRAGMENT_TAG )
           .commit();
     }

     fragmentManager.executePendingTransactions();

     m_mapFragment = mapFragment;
     configureMap();
  }

  protected void configureMap ()
  {
     if ( m_mapFragment != null && m_eventMapFeature != null && m_eventMapFeature.haveValidLocation() == true )
     {
        final EventAlbumItemFragment localThis = this ;

        m_mapFragment.getMapAsync( new OnMapReadyCallback()
        {
           @Override
           public void onMapReady( GoogleMap googleMap )
           {
              final View mapContainer = getView().findViewById( R.id.mapview_container );
              final int mapWidth = mapContainer.getWidth();
              final int mapHeight = mapContainer.getHeight();

              // we have our map and the coordinates should be correct. Make map visible and proceed
              mapContainer.setVisibility( View.VISIBLE );

              // This line below crashes because view is NULL after rotation. 
              // Left here to illustrate what I believe to be the root cause of the issue
              // m_mapFragment.getView().setVisibility( View.VISIBLE );

              // Remove all overlays from the map
              googleMap.clear();

              final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

              final float zoom = (float)GeoUtils.getZoomForMetersWide( MAP_MINIMUM_METER_SPAN,
                      mapWidth / displayMetrics.scaledDensity,
                      m_eventMapFeature.getCoordinate().latitude );

              // Change the map bounds
              CameraUpdate cameraUpdate;

              // draw some items on the map
              final LatLngBounds bounds = m_eventMapFeature.getBounds();

              cameraUpdate = CameraUpdateFactory.newLatLngZoom( bounds.southwest, zoom );

              googleMap.moveCamera( cameraUpdate );
           }
        } );
     }
  }

  /** Assign the map fragment variable on resume. This is necessary for orientation change events */
  @Override
  public void onResume ()
  {
     super.onResume();

     // If the map fragment hasn't been set, but this is the current item, get the map fragment from the parent
    final EventReportAlbumActivity activity = (EventReportAlbumActivity)getActivity();

      if( m_mapFragment == null && this == activity.getCurrentItemFragment() )
      {
         setMapFragment( activity.getMapFragment() );
      }

     //
     configureMap();
  }

}

更新:上面的setMapFragment在有多个页面并且用户页面在旋转后通过相册时也会崩溃

经过一些测试,我也在相册有多个页面的情况下测试了这段代码。没有旋转,我可以毫无问题地滚动相册。但是,如果我旋转设备并尝试滚动相册,应用程序就会崩溃。同样,使用版本 19 的支持库,此代码按预期工作。这只是在升级到版本 22 后才开始发生。

在EventAlbumItemFragment中的setMapFragment()方法中:

fragmentManager.executePendingTransactions();

我得到以下堆栈跟踪:

java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:4020)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.view.View$1.onClick(View.java:4015)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.set(int, java.lang.Object)' on a null object reference
    at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1192)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
    at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1235)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:710)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
    at crc.carsapp.fragments.MapAlbumItemFragment.setMapFragment(MapAlbumItemFragment.java:78)
    at crc.carsapp.listeners.OnMapAlbumScrollListener.onPageSelected(OnMapAlbumScrollListener.java:46)
    at crc.carsapp.listeners.OnEventAlbumViewScrollListener.onPageSelected(OnEventAlbumViewScrollListener.java:34)
    at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1786)
    at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:568)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:505)
    at crc.carsapp.activities.AlbumActivity.scrollToPrevious(AlbumActivity.java:133)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.view.View$1.onClick(View.java:4015)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

【问题讨论】:

  • 您的 Google Play 服务是最新的吗?
  • 是的,是7.5.0版

标签: android google-maps screen-rotation supportmapfragment


【解决方案1】:

因此,上述代码存在 2 个问题。首先(也是最关键的),Activity (EventReportAlbumActivity) 负责创建地图片段(及其关联的片段保持对它的引用)。在轮换期间,活动(当然)被销毁并重新创建。因此,对 getMapFragment 的后续调用将生成一个新的 SupportMapFragment。同时,显示地图的片段仍然具有对先前地图片段的引用。

每当在 setMapFragment 中调用 remove 时,它​​都会引用旧的 SupportMapFragment 并因此引发异常。

这也导致地图在旋转后无法正常显示。

解决方法是在 setMapFragment 中移除对 remove 的调用(ViewPager 处理从 onPageSelected 中的前一页中删除)并根据需要在 setMapFragment 中添加“新”SupportMapFragment。

在升级到版本 22 之前原始代码的工作方式是一个谜,因为无论支持版本如何,这似乎都是一个错误。而且,该解决方案仍然感觉不太正确,因为 SupportMapFragment 将其 setRetainInstance 设置为 true,这意味着我们希望它在片段的整个生命周期中存在,但是当在旋转期间重新创建它时,活动最终会覆盖它。所以,如果有人有更优雅的解决方案,我很想听听。

相关更新代码如下:

public class EventAlbumItemFragment extends Fragment
{
   public void setMapFragment ( @Nullable SupportMapFragment mapFragment )
  {
     final FragmentManager fragmentManager = getChildFragmentManager();

     // The working assumption is that our map fragment, if already added, is added to
     // our current fragment.
     if ( mapFragment != null && mapFragment.isAdded() == false )
     {
        // Add the map view to the current view
        fragmentManager.beginTransaction()
           .add( R.id.mapview_container, mapFragment, MAP_FRAGMENT_TAG )
           .commit() ;
        fragmentManager.executePendingTransactions();
     }

     m_mapFragment = mapFragment;
     configureMap();
  }

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

     // We can get here in (at least) 3 different ways:
     // 1. The first time this fragment is created.
     //       In this case, we will not have a local reference to a map fragment and will will add
     //       it if we are the currently displaying fragment.
     // 2. After rotation
     //       Because the actual map fragment is maintained by our activity, the map fragment will
     //       have been recreated and our reference to it will no longer be valid.
     // 3. After a pause event (such as the device going into power saving mode).
     //       We should still have a reference to the current map fragment and it should be
     //       valid. We can test that by checking if the map fragment is attached. This is done
     //       in the setMapFragment() method.
     //
     final AlbumActivity activity = (AlbumActivity)getActivity();

     Fragment currentFragment = activity.getCurrentItemFragment() ;

     if ( m_mapFragment == null && this == currentFragment )
     {
        setMapFragment( activity.getMapFragment() );
     }
     else if ( m_mapFragment != null )
     {
        setMapFragment( activity.getMapFragment() );
     }

     configureMap();
   }
}

【讨论】:

  • 我发现了一个与嵌套片段相关的问题和一个出现在版本 20+ 的支持库中的已知错误。我在应用程序的另一部分有类似的代码,它只使用片段来管理 SupportMapFragment,并且在那里也出现了旋转问题。此博客文章包含对支持库中的错误的描述以及感兴趣的人的修复:ideaventure.blogspot.com.au/2014/10/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多