当我尝试向我的实时Firebase数据库写入,更新,删除或更改任何类型的数据时,系统会自动重新创建活动。我在线检查了解决方案,但所有方法均无效。关于SingleValueEventListener的信息。我可能做错了什么?下面是我的代码。
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled;
assert lm != null;
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
relativelayout = findViewById(R.id.relativelayout);
signoutRef = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("device_token");
username = findViewById(R.id.username);
greetings = findViewById(R.id.greetings);
sign_out = findViewById(R.id.sign_out);
sign_out.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signoutRef.setValue("no_token");
Toast.makeText(MainActivity.this, "Signing out...", Toast.LENGTH_SHORT).show();
FirebaseAuth.getInstance().signOut();
Intent log_out = new Intent(MainActivity.this, LoginActivity.class);
startActivity(log_out);
finish();
}
});
ongoing_task = findViewById(R.id.latest_task);
ongoing_task.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PressRef = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).child("Ongoing");
PressRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String c_id = dataSnapshot.child("cu_id").getValue().toString();
Intent choose_mua_intent = new Intent(MainActivity.this, OngoingTask.class);
choose_mua_intent.putExtra("customer_id", c_id);
startActivity(choose_mua_intent);
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
databaseReference = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d("Culprit", "Yes, it is me 1");
String image = dataSnapshot.child("image").getValue().toString();
String name = dataSnapshot.child("username").getValue().toString();
if (!image.equals("default")) {
Picasso.with(getApplicationContext()).load(image).placeholder(R.drawable.no_image_two).into(profile);
}
username.setText(name);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
location_switch = findViewById(R.id.location_switch);
switchRef = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("online");
currentUserRef = FirebaseDatabase.getInstance().getReference("MakeUp_Artists").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
setUpLocation();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
switchRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d("Culprit", "Yes, it is me 2");
String online = dataSnapshot.getValue().toString();
if (online.equals("true")) {
location_switch.setChecked(true);
FirebaseDatabase.getInstance().goOnline();
} else {
currentUserRef.onDisconnect().removeValue();
location_switch.setChecked(false);
FirebaseDatabase.getInstance().goOffline();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
if (gps_enabled) {
location_switch.setClickable(true);
} else {
location_switch.setClickable(false);
}
location_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean IsOnline) {
if (IsOnline) {
switchRef.setValue(true);
FirebaseDatabase.getInstance().goOnline();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildLocationRequest();
buildLocationCallback();
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallBack, Looper.myLooper());
displayLocation();
Snackbar.make(relativelayout, "You are currently available", Snackbar.LENGTH_LONG).show();
} else {
switchRef.setValue(false);
FirebaseDatabase.getInstance().goOffline();
fusedLocationProviderClient.removeLocationUpdates(mLocationCallBack);
currentUserRef.onDisconnect().removeValue();
mCurrent.remove();
mMap.clear();
Snackbar.make(relativelayout, "You just went offline", Snackbar.LENGTH_LONG).show();
}
}
});
mArtists = FirebaseDatabase.getInstance().getReference("MakeUp_Artists");
geoFire = new GeoFire(mArtists);
onGoingRef = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Ongoing");
onGoingRef.addListenerForSingleValueEvent(new ValueEventListener() {
@SuppressLint("RestrictedApi")
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Culprit", "Yes, it is me 3");
String ongoing = dataSnapshot.child("alert_issue").getValue().toString();
if (ongoing.equals("Your request was accepted")) {
location_switch.setVisibility(View.GONE);
ongoing_task.setVisibility(View.VISIBLE);
} else {
location_switch.setVisibility(View.VISIBLE);
ongoing_task.setVisibility(View.INVISIBLE);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
buildLocationCallback();
buildLocationRequest();
if (location_switch.isChecked()) {
displayLocation();
}
}
}
}
private void setUpLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CALL_PHONE
}, MY_PERMISSION_REQUEST_CODE);
} else {
buildLocationRequest();
buildLocationCallback();
if (location_switch.isChecked()) {
displayLocation();
}
}
}
private void buildLocationCallback() {
mLocationCallBack = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
CommonClass.mLastLocation = location;
displayLocation();
}
}
};
}
private void buildLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
private void displayLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
CommonClass.mLastLocation = location;
if (CommonClass.mLastLocation != null) {
if (location_switch.isChecked()) {
final double latitude = CommonClass.mLastLocation.getLatitude();
final double longitude = CommonClass.mLastLocation.getLongitude();
geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(),
new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
Log.d("Culprit", "Yes, it is me 4");
if (mCurrent != null)
mCurrent.remove();
mCurrent = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Your current location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.rec)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15.0f));
/*rotateMarker(mCurrent, 360, mMap);*/
}
});
}
} else {
Log.d("ERROR", "Cannot get your location");
}
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
try {
boolean isSuccess = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(this, R.raw.makeup_aid_map)
);
if (!isSuccess)
Log.e("MUA - CUSTOMER", "Map style failed!");
} catch (Resources.NotFoundException ex) {
ex.printStackTrace();
}
mMap = googleMap;
mMap.setTrafficEnabled(false);
mMap.setIndoorEnabled(false);
mMap.setBuildingsEnabled(false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildLocationRequest();
buildLocationCallback();
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallBack, Looper.myLooper());
}
我可能做错了什么?