android - Google Maps v2 - onclick 监听器在方向更改后不响应

标签 android android-maps-v2

我在我的应用程序中使用 Google Maps V2,当我将方向更改为横向(或基本上更改方向)时,onMapClick 方法不响应并且即使在方向再次更改后也不会响应。我知道我可以通过设置 configChange:orientation|screenSize 来避免这个奇怪的错误,但是当方向改变时,我的对话布局不会将它们的布局从纵向布局更改为横向布局。 这一切都在一项 Activity 中。有没有人遇到过这个问题? map 有什么问题?我不明白问题出在哪里,为什么应该取消注册听众。 我在 onCreateMethod 中注册它: @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    DisplayMetrics dm = getResources().getDisplayMetrics();
    screenWidth = dm.widthPixels;
    screenHeight = dm.heightPixels;

    FontUtils.setCustomFont(this, (ViewGroup) getWindow().getDecorView());

    RelativeLayout topBar = (RelativeLayout) findViewById(R.id.topBar);
    LinearLayout placeTextLayout =    (LinearLayout)topBar
                                                  .findViewById(R.id.placeTextLayout);
    placeTextView = (TextView) placeTextLayout.findViewById(R.id.placeText);

    sharedPrefs = getSharedPreferences
                              (SkyConstants.PREFS_NAME,Context.MODE_PRIVATE);
    String place = sharedPrefs.getString
                  (SkyConstants.PREF_LOCATION_ID,SkyConstants.PREF_LOCATION_DEFAULT);

    dateTextView = (TextView) topBar.findViewById(R.id.dateText);
    daysTextView = (TextView) topBar.findViewById(R.id.timeText);

    updateTopBar();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded()
{
    if (mMap == null)
    {
        mMap = ((SupportMapFragment)getSupportFragmentManager()
                                                 .findFragmentById(R.id.map)).getMap();     
    }
    if (mMap != null)
    {
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setZoomGesturesEnabled(true);          
        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

        mMap.setOnMapClickListener(new OnMapClickListener()
        {
            @Override
            public void onMapClick(LatLng point)
            {
            //do something  

            }

        });
    }
}

最佳答案

这是由于为方向创建了 View ,如果您处于纵向模式,然后更改为横向,它会再次创建 View ,您需要再次设置 onClickListener。

如果您以横向模式启动 Activity,则会发生同样的情况。

关于android - Google Maps v2 - onclick 监听器在方向更改后不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15552315/

相关文章:

android - 如何找到 gradle 使用的是哪个 checkstyle 版本?

java - 将目录、子目录和文件复制到 SD 卡 - android

java - 谷歌地图扩展

android - 有没有办法在 ImageView 上添加标记

android - 谷歌地图标记未显示

Android map - animateCamera() 方法无法正常工作

java - fragment 中的 CirclePageIndicator

android - 通过获取每个 child 的位置动态地将 child 添加到 LinearLayout

android - Gradle 没有指向指定的版本

android - google mapView 样式不适用于 maptype=TERRAIN 的 Android