【问题标题】:Android Google Maps transition from portrait to landscape and vice versaAndroid 谷歌地图从纵向过渡到横向,反之亦然
【发布时间】:2015-03-11 17:26:31
【问题描述】:

我的应用程序仅在 Google 地图上显示地图。我的问题是当我改变屏幕的位置(我在平板电脑上安装的应用程序)时,在显示地图之前会出现一个白屏。

我的配置; res/layout/main.xml 和 res/layout-land/main.xml 从纵向切换到横向模式。

应该添加什么或做些什么来解决这个问题?

【问题讨论】:

  • 您可以发布您的活动的 onCreate 和布局吗?让我们看看你是否在那里做了一些过度的工作。
  • schemas.android.com/apk/res/android" android:id="@+id/map" android:name ="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" />
  • 编辑您的问题。 cmets 空间不足
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);尝试 { initilizeMap(); } 捕捉(异常 e){ e.printStackTrace(); } }
  • private void initilizeMap(){ if (googleMap == null) { googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); if (googleMap == null) { Toast.makeText(getApplicationContext(), "impossible de créér googleMaps", Toast.LENGTH_LONG).show(); } } } @Override protected void onResume(){ super.onResume();初始化映射(); }

标签: android


【解决方案1】:

由于您已经在布局文件中指定了地图片段,您可以使用findFragmentById in onCreate 安全地找到它,如下所示:

MapFragment mMapFragment; // this is neither GoogleMap or MapView!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
    initializeMap();
}

@Override
public void onResume() {
    super.onResume();
    // map init was already done in onCreate, will not do it twice
}

然后,当您需要 GoogleMap 对象时,您需要它并通过回调传递它。当回调执行时,地图保证不为空。这将从 Google Play 服务库 v6.5 开始工作。

private void initializeMap() {
    mMapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            // googleMap will never be null here
            // do not store it in a variable, always make a getMapAsync call
            // do your map setup here
        }
    }
}

这是最快的,所以你不应该遇到任何闪烁。

注意:在您提到的原始帖子中,纵向和横向的布局不同。如果您确实这样做,请确保两者都包含相应的 fragment ID,否则您一定会遇到异常。您还提到布局是 main.xml 而不是 activity_main.xml,因此请确保使用您想要的布局。

【讨论】:

  • 你的纵向和横向布局有什么区别吗?请用格式化代码更新问题,不要在 cmets 中发布,它很难阅读。
【解决方案2】:

我在阅读这篇文章时解决了我的问题(Android 上的活动重启)Activity restart on rotation Android

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2022-07-07
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多