【问题标题】:Google Maps v2 - onclick listener does not respond after orientation changesGoogle Maps v2 - onclick 监听器在方向更改后没有响应
【发布时间】:2013-03-11 05:53:34
【问题描述】:

我在我的应用程序中使用 Google Maps V2,当我将方向更改为横向(或基本上更改方向)时,onMapClick 方法不响应,即使在方向再次更改后也不响应。我知道我可以通过设置 configChange:orientation|screenSize 来避免这个奇怪的错误,但是当方向改变时,我的对话布局不会将它们的布局从 layout-portrait 更改为 layout-landscape。 这一切都在一项活动中。有没有人遇到过这个问题?地图有什么问题?我不明白问题出在哪里,为什么应该取消注册听众。 我在 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  

            }

        });
    }
}

【问题讨论】:

    标签: android android-maps-v2


    【解决方案1】:

    这是由于创建了方向视图,如果您是纵向模式,并且您更改为横向,它会再次创建视图,您需要再次设置 onClickListener。

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

    【讨论】:

    • 我在onCreate方法中设置了onMap点击监听器,可以清楚的看到设置监听器的方法,每次改变方向时都会调用。但是听者不知何故消失了……我单击并单击,没有任何反应。如果我将方向改回纵向,听众仍然会迷路......
    【解决方案2】:

    在 onCreate() 方法中将 map 设置为 null 有助于我处理系统的方向更改。 所以首先我设置 mMap=null 然后我设置地图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-30
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多