我正在开发一个应用程序,该应用程序有一个名为 PostCar 的方法,调用该方法时应该创建一个数据库引用并将一个子项添加到我的 Firebase 控制台贷方表中,一个包含用户 ID 和位置的子项。问题是位置一直被标记为空。我的 PostCar 方法中的代码读取“geoFire.setLocation(userId,new GeoLocation(mlastlocation.getLatitude(),mlastlocation.getLongitude()));”不断抛出错误“尝试在空对象引用上调用虚拟方法‘double android.location.location.getlatitude()’”。设备位置已打开,我检查了位置权限。这是下面的代码。
package com.example.movir;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class LendersMenuPageActivity extends AppCompatActivity {
private String userId;
private FirebaseAuth mAuth;
private LocationManager lm;
Location mlastlocation;
LocationRequest locationRequest;
LocationCallback locationCallback;
private Button lendcarButton;
final LendCarForm cardlend = new LendCarForm(userId,"brand new Toyota","Toyota","Camry S9");
private FusedLocationProviderClient fusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lenders_menu_page);
mAuth = FirebaseAuth.getInstance();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
FirebaseUser currentUser = mAuth.getCurrentUser();
userId = currentUser.getUid();
DatabaseReference lendersReference = FirebaseDatabase.getInstance().getReference("Users").child("Lenders").child(userId);
lendcarButton = findViewById(R.id.lenders_car_button);
lendcarButton.setOnClickListener(onClick);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
if(getApplicationContext()!=null) {
mlastlocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
}
}
};
mlastlocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
@Override
protected void onStart() {
super.onStart();
checkLocationPermission();
}
private void startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback,
Looper.getMainLooper());
}
public View.OnClickListener onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(LendersMenuPageActivity.this,"hello",Toast.LENGTH_LONG);
postCar();
}
};
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
new AlertDialog.Builder(this)
.setTitle("give permission")
.setMessage("give permission messaage")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(LendersMenuPageActivity.this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
}
})
.create()
.show();
} else {
ActivityCompat.requestPermissions(LendersMenuPageActivity.this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
}
}
}
public void postCar(){
DatabaseReference carsForRent = FirebaseDatabase.getInstance().getReference("Users").child("Lenders").child(userId);
GeoFire geoFire = new GeoFire(carsForRent);
geoFire.setLocation(userId,new GeoLocation(mlastlocation.getLatitude(),mlastlocation.getLongitude()));
carsForRent.setValue(cardlend);
}
}
答案 0 :(得分:0)
您是否正在使用 Xcode IOS 模拟器对此进行测试?模拟器实际上无法访问您的真实位置,因此在查询时它将返回 null,除非手动指定(功能选项卡、位置、自定义位置。)
答案 1 :(得分:0)
fusedapi 对我不起作用的原因是因为我没有使用谷歌地图项目。所以我overode onlocatiochanged 代替。这对我有用,但你必须扩展 onlcationchanged 和 googleapi