【问题标题】:I am not able to detect long click on my phone or emulator我无法检测到手机或模拟器上的长按
【发布时间】:2018-07-17 22:34:14
【问题描述】:

当我长时间点击手机或模拟器时,我无法检测到任何东西。此外,我没有收到任何错误或 Logcat 消息。我目前正在使用 Android Studio 3.1.3。

package com.example.gaurangadas.test;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMapLongClickListener {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }


    @Override
    public void onMapLongClick(LatLng latLng) {
        Toast.makeText(this, "HELLO", Toast.LENGTH_SHORT).show();
        Log.i("LONG CLICK","DETECTED");
    }
}

【问题讨论】:

标签: android google-maps


【解决方案1】:
mMap.setOnMapLongClickListener(this);

把它放在你的 onMapReady() 之后 mMap = 谷歌地图; 这会将您的 onMapLongClick 注册到 mMap 谷歌地图。

【讨论】:

  • 在 onMapReady 回调中做可能更好?
  • 现在我收到以下错误“java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gaurangadas.test/com.example.gaurangadas.test.MapsActivity}: java.lang .NullPointerException:尝试在空对象引用上调用虚拟方法“void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap$OnMapLongClickListener)”
  • @H.Taras 我同意,我们明白原因了 :) mMap 尚未初始化。我会编辑。
猜你喜欢
  • 2014-01-19
  • 2016-02-11
  • 2020-02-18
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多