gpt4 book ai didi

java - Android Studio 监听器导致错误

转载 作者:行者123 更新时间:2023-12-02 01:39:04 26 4
gpt4 key购买 nike

我正在尝试实现一个监听器来监视对标记的所有点击(Google Maps API),以允许在标记点击事件触发器上调用函数。但是,每次我包含事件监听器代码时,应用程序都会崩溃。

事件监听器 - setOnMarkerClickListener

如何正确实现此监听器并处理崩溃?

我已经使用的代码是:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {

// TODO Auto-generated method stub
if(marker.equals(ibrox)){
Log.w("Click", "test");
return true;
}
return false;

}
});

我希望程序能够运行而不会崩溃,并且函数能够记录“测试”。

编辑:ibrox 变量的值 -

private static final LatLng ibrox = new LatLng(55.8522161, -4.3138359);

整个代码:

package com.example.googlemaps;

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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

private static final LatLng ibrox = new LatLng(55.8522161, -4.3138359);
private Marker mIbrox;

@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);

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {

// TODO Auto-generated method stub
if(marker.equals(ibrox)){
Log.w("Click", "test");
return true;
}
return false;

}
});

}

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

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(40.7589, -73.9851);
mIbrox = mMap.addMarker(new MarkerOptions()
.position(ibrox)
.title("Ibrox"));

mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in a place"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

最佳答案

请确保mMap不为空。您需要在 onMapReady 方法中设置监听器。

按照这个例子:- https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/java/com/example/mapdemo/MarkerDemoActivity.java

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {

// TODO Auto-generated method stub
if(marker.equals(ibrox)){
Log.w("Click", "test");
return true;
}
return false;

}
});
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(40.7589, -73.9851);
mIbrox = mMap.addMarker(new MarkerOptions()
.position(ibrox)
.title("Ibrox"));

mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in a place"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

关于java - Android Studio 监听器导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54737801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com