移动相机方法上的谷歌地图空对象引用[关闭]
Posted
技术标签:
【中文标题】移动相机方法上的谷歌地图空对象引用[关闭]【英文标题】:Google Maps null object reference on move camera method [closed] 【发布时间】:2021-08-09 15:36:03 【问题描述】:我正在尝试使用谷歌地图,即使用标记获取当前位置、搜索位置并根据搜索到的位置添加标记。我收到此错误
尝试在空对象引用上调用虚拟方法“void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)”
我不明白为什么我会得到这个,我认为我实例化我的 GoogleMap 变量可能有问题,但我不知道如何解决这个问题。这是我的代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
private GoogleMap mMap;
Location mLocation;
Location sLocation;
FusedLocationProviderClient client;
private static final int Request_Code = 101;
EditText txtSrch;
ImageButton btnSrch;
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.
client = LocationServices.getFusedLocationProviderClient(this);
getLastLocation();
btnSrch.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
geolocate();
);
我做了这些方法:
private void geolocate()
String search = txtSrch.getText().toString().trim();
Geocoder geocoder = new Geocoder(MapsActivity.this);
List<Address> list = new ArrayList<>();
try
list = geocoder.getFromLocationName(search, 1);
catch (IOException e)
e.printStackTrace();
if(list.size() > 0)
Address address = list.get(0);
moveCamera(new LatLng(address.getLatitude(), address.getLongitude()), 6, "Searched Location");
private void moveCamera(LatLng latLng, float zoom, String title)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title(title);
mMap.addMarker(options);
private void getLastLocation()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]
android.Manifest.permission.ACCESS_FINE_LOCATION, Request_Code);
return;
Task<Location> task = client.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>()
@Override
public void onSuccess(Location location)
if(location != null)
mLocation = location;
Toast.makeText(getApplicationContext(),mLocation.getLatitude()+""+mLocation.getLongitude(), Toast.LENGTH_SHORT).show();
double myLong = mLocation.getLongitude();
double myLat = mLocation.getLatitude();
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(MapsActivity.this);
);
@Override
public void onMapReady(GoogleMap googleMap)
LatLng latLng = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("You Are Here");
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 6));
googleMap.addMarker(markerOptions);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
switch (requestCode)
case Request_Code:
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
getLastLocation();
break;
老实说,我认为实例化 mMap 变量存在问题,我只是不知道该怎么办。任何帮助将不胜感激
【问题讨论】:
这能回答你的问题吗? What is a NullPointerException, and how do I fix it?I just don't know what to do about it
尝试使用时应确保它不为空
看起来你从未初始化过mMap
【参考方案1】:
是的。你是对的。需要启动你的 mMap。
@Override
public void onMapReady(GoogleMap googleMap)
mMap=googleMap;
//And before move camera, check mMap is not null
private void moveCamera(LatLng latLng, float zoom, String title)
if(mMap!=null)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title(title);
mMap.addMarker(options);
【讨论】:
以上是关于移动相机方法上的谷歌地图空对象引用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚方法'double android.location.Location.getLatitude()'[duplicate]
Google Vision API - 空对象引用上的人脸方法
java.lang.NullPointerException:尝试在 OnPostExecute() 上的空对象引用错误上调用接口方法 - AsyncTask
android.os.Looper对空对象引用的android.content.Context.getMainLooper()