我有这段代码:
if (distance < 1f) // or some distance
{
Gameobject mygo = GameObject.Find("GameManager"); // or some other method to get gameobject.
mygo.GetComponent<casting>().enabled = false;
}
行:location.addCompleteListener被Android Studio标记为未选中,它会使应用崩溃。所以我必须以某种方式“检查”它。 这是在Android Studio的最新更新补丁之后发生的。
我该如何解决这个问题?
答案 0 :(得分:1)
如果我有50个以上的代表,我会发表评论。
无论如何,您可以继续使用
.addOnCompleteListener(new OnCompleteListener<Location>(){
});
通过,添加到@ nope4561759所说的内容,将<Location>
添加到onComplete()重写方法的Task参数上,如下所示:public void onComplete(@NonNull Task<Location> task) {…}
答案 1 :(得分:0)
尝试更改:
final Task location = mFusedLocationClient.getLastLocation();
location.addOnCompleteListener(this, new OnCompleteListener() {
到此:
final Task<Location> location = mFusedLocationClient.getLastLocation();
location.addOnCompleteListener(this, new OnCompleteListener<Location>() {
答案 2 :(得分:0)
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the devices current location");
FusedLocationProviderClient mfusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
try {
if (mLocationPermissionGranted) {
final Task<Location> location = mfusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(this, new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: found location");
try {
Location currentLocation = (Location) task.getResult();
//globalLatitude = currentLocation.getLatitude();
//globalLongitude = currentLocation.getLongitude();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Please turn on your GPS", Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "onComplete: Location not found");
Toast.makeText(MainActivity.this, "Cannot find current location", Toast.LENGTH_SHORT).show();
}
}
});
}
} catch (SecurityException e) {
Log.e(TAG, "getDeviceLocation: Security Exception" + e.getMessage());
}
}
addOnCompleteListener未被选中,导致应用程序崩溃。