我检查了我的另一个应用程序,该应用程序从实时数据库中检索位置,用户的位置根本不反映在地图上。我一直在代码中配置数据库引用,以获得用户位置的正确路径,但应用程序一直因NullPointerExceptions而崩溃。我应该如何引用正确的子键,尤其是用户的唯一键?
代码如下:
private GoogleMap mMap;
private ActivityRetrieveMapsBinding binding;
private Location mLocation;
private LocationManager mLocationManager;
private LocationRequest mLocationRequest;
private com.google.android.gms.location.LocationListener listener;
private LocationManager locationManager;
GoogleApiClient mGoogleApiClient;
public static final int REQUEST_CHECK_SETTINGS = 1001;
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityRetrieveMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 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);
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
checkLocation();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
final LocationSettingsStates LS_state = locationSettingsResult.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(RetrieveMapsActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
private boolean checkLocation() {
if(!isLocationEnabled()){
showAlert();
}
return isLocationEnabled();
}
private void showAlert() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
private boolean isLocationEnabled() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationkey = reference.child("Users").child(uid).child("User Location");
locationkey.addValueEventListener(new ValueEventListener () {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
String locationKey = dataSnapshot.getKey();
LocationSend send = snapshot.getValue(LocationSend.class);
LatLng location = new LatLng(send.latitude, send.longitude);
mMap.addMarker(new MarkerOptions()
.position(location)
.title(getCompleteAddress(send.latitude, send.longitude)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,14F));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.d(TAG, error.getMessage());
}
});
}
private String getCompleteAddress(double Latitude,double Longitude){
String address = "";
Geocoder geocoder = new Geocoder(RetrieveMapsActivity.this, Locale.getDefault());
try{
List<Address> addresses = geocoder.getFromLocation(Latitude,Longitude,1);
if(address!=null){
Address returnAddress = addresses.get(0);
StringBuilder stringBuilderReturnAddress = new StringBuilder("");
for(int i=0; i<=returnAddress.getMaxAddressLineIndex();i++){
stringBuilderReturnAddress.append(returnAddress.getAddressLine(i)).append("\n");
}
address = stringBuilderReturnAddress.toString();
}
else{
Toast.makeText(this, "Address not found", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return address;
}
@Override
public void onLocationChanged(@NonNull Location location) {
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
if (requestCode == REQUEST_CHECK_SETTINGS) {
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings but chose not to
break;
default:
break;
}
}
}
}
使用以下引用时:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid).child("User Location");
请注意,没有必要遍历结果。您只应再次对DataSnapshot
对象调用.child()
,如下代码行所示:
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
double latitude = snapshot.child("latitude").getValue(Double.class);
double longitude = snapshot.child("longitude").getValue(Double.class);
Log.d("TAG", "latitude, longitude");
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
logcat中的结果将是:
15.44318, 120.7148767
我试图从Azure密钥库中检索一个秘密(不使用凭据,如本教程:示例): 但是当我调用时,我得到一个 我的密钥库url的格式类似于“https://my-keyvault.vault.azure.net”。
这是我的第二个版本,我试图从Firebase检索代码,并用它做一些事情。这是我的第二种方式: 这将崩溃,并出现错误代码: 未能将类型“__NSCFString”(0x10A77F4A0)的值强制转换为“NSDictionary”(0x10A780288)。在“更新”行。这是我的第一次尝试: 打印更多数据: -路径通道引用:可选(https://x.com/channels/-kegkajavh6u
Parse将在年底关闭,所以我决定开始使用Firebase。我需要用3个字段实现注册过程:电子邮件、用户名、密码(电子邮件 由于Firebase没有提供像Parse这样管理用户名的简单方法,我决定只使用电子邮件/密码注册并保存一些附加数据,例如用户名。这是我的用户数据结构: 但是,我想做的是使用户名唯一,并在创建帐户之前进行检查。以下是我的规则: 非常感谢您的帮助
问题内容: 我在使用注释的缓存键在Spring中发生冲突时遇到问题。例如,使用以下两种方法: 这是我的缓存配置,其中添加了一个和一个bean: 由于某种原因,缓存键始终设置为方法中的参数,而不是方法的结果,从而导致两个方法返回相同的缓存结果。 我知道我可以在每个注释上手动指定键,但是对于我想要缓存的每种方法来说,这似乎有点扩展。 编辑 我注意到,将注释内的选项设置为我的bean的名称可以解决此问题
我创建了一个密钥库,它包括私钥和公钥。我需要使用java代码检索我的公钥和私钥。我在这里找到了一个检索私钥的方法。但我找不到检索公钥的方法。有人能告诉我一种取出公钥的方法吗
我正在尝试从cloud Firestore的Flutter中的以下路径获取数据,我有以下结构 followers/{currentuser}/userfollower/{user_id} 用户/user_id/user_details 我正在尝试获取当前用户的所有关注者的详细信息。有人能帮忙吗? 我能得到第一部分是因为 我不知道如何循环通过所有id从用户集合中获取数据。我尝试了以下代码,但没有成功