我正在尝试关闭我的位置,但是单击后显示给我一个错误:尝试调用虚拟方法'void com.google.android.gms.maps.model.Marker.remove() 请你帮助我好吗。我叫Common.java,但是标记返回null。 mCurrent.remove();一片空白。我正在使用libray实现'androidx.fragment:fragment:1.0.0'
我的DriverHome.java
public class DriverHome extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
OnMapReadyCallback
{
private GoogleMap mMap;
FusedLocationProviderClient fusedLocationProviderClient;
LocationCallback locationCallback;
//Play Services
private static final int MY_PERMISSION_REQUEST_CODE = 7000;
private static final int PLAY_SERVICE_RES_REQUEST = 7001;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private static int UPDATE_INTERVAL = 5000;
private static int FATEST_INTERVAL = 3000;
private static int DISPLACEMENT = 10;
DatabaseReference drivers;
GeoFire geoFire;
Marker mCurrent;
MaterialAnimatedSwitch location_switch;
SupportMapFragment mapFragment;
//Car animation
private List<LatLng> polyLineList;
private Marker carMarker;
private float v;
private double lat,lng;
private Handler handler;
private LatLng startPosition,endPosition,currentPosition;
private int index,next;
private PlaceAutocompleteFragment places;
AutocompleteFilter typeFilter;
private String destination;
private PolylineOptions polylineOptions,blackPolylineOptions;
private Polyline blackPolyline,greyPolyline;
private IGoogleAPI mService;
//Presense System
DatabaseReference onlineRef, currentUserRef;
//Firebase
FirebaseStorage firebaseStorage;
StorageReference storageReference;
Runnable drawPathRunnable = new Runnable() {
@Override
public void run() {
if (index<polyLineList.size()-1)
{
index++;
next = index+1;
}
if (index <polyLineList.size()-1)
{
startPosition = polyLineList.get(index);
endPosition = polyLineList.get(next);
}
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1);
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
v = valueAnimator.getAnimatedFraction();
lng = v*endPosition.longitude+(1-v)*startPosition.longitude;
lat = v*endPosition.latitude+(1-v)*startPosition.latitude;
LatLng newPos = new LatLng(lat,lng);
carMarker.setPosition(newPos);
carMarker.setAnchor(0.5f,0.5f);
carMarker.setRotation(getBearing(startPosition,newPos));
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder()
.target(newPos)
.zoom(15.5f)
.build()
));
}
});
valueAnimator.start();
handler.postDelayed(this,3000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("MoaguiTaxi");
setSupportActionBar(toolbar);
//Init Firebase Storage
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View navigationHeaderView = navigationView.getHeaderView(0);
TextView txtName = (TextView)navigationHeaderView.findViewById(R.id.txtDriverName);
CircleImageView imageAvatar = (CircleImageView)navigationHeaderView.findViewById(R.id.image_avatar);
//We can access from Common.currentDriver because in MainActivity, after login , we have set this data
txtName.setText(Common.currentDriver.getName());
//But with Avatar , we just check it with null or empty
if (Common.currentDriver.getAvatarUrl() != null
&& !TextUtils.isEmpty(Common.currentDriver.getAvatarUrl())) {
Picasso.with(this)
.load(Common.currentDriver.getAvatarUrl())
.into(imageAvatar);
}
//Paste Here
handler = new Handler();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Presense System
AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
@Override
public void onSuccess(Account account) {
onlineRef = FirebaseDatabase.getInstance().getReference(".info/connected");
currentUserRef = FirebaseDatabase.getInstance().getReference(Common.driver_tbl)
.child(account.getId());
onlineRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//We will remove value from Driver tbl when driver disconnected
currentUserRef.onDisconnect().removeValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onError(AccountKitError accountKitError) {
}
});
location_switch = (MaterialAnimatedSwitch)findViewById(R.id.location_switch);
location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isOnline) {
if (isOnline)
{
FirebaseDatabase.getInstance().goOnline(); // set connected when switch to on
if (ActivityCompat.checkSelfPermission(DriverHome.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(DriverHome.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
buildLocationCallBack();
buildLocationRequest();
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest,locationCallback, Looper.myLooper());
displayLocation();
Snackbar.make(mapFragment.getView(),"Estás en Línea",Snackbar.LENGTH_SHORT)
.show();
}
else
{
FirebaseDatabase.getInstance().goOffline(); //Set disconnected when switch to off
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
mCurrent.remove();
mMap.clear();
if (handler != null)
handler.removeCallbacks(drawPathRunnable);
Snackbar.make(mapFragment.getView(),"Estás desconectado",Snackbar.LENGTH_SHORT)
.show();
}
}
});
My class Common.java
public class Common {
public static final String driver_tbl = "Drivers";
public static final String user_driver_tbl = "DriversInformation";
public static final String user_rider_tbl = "RidersInformation";
public static final String pickup_request_tbl = "PickupRequest";
public static final String token_tbl = "Token";
public static final int PICK_IMAGE_REQUEST = 9999;
public static Driver currentDriver;
public static Location mLastLocation=null;
public static final String baseURL = "https://maps.googleapis.com";
public static final String fcmURL = "https://fcm.googleapis.com/";
public static final String user_field = "usr";
public static final String pwd_field = "pwd";
public static double base_fare = 2.00; //base on Uber fee at NewYork
private static double time_rate = 0.20;
private static double distance_rate = 0.50;
public static double formulaPrice(double km,double min)
{
return base_fare+(distance_rate*km)+(time_rate*min);
}
public static IGoogleAPI getGoogleAPI()
{
return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
}
public static IFCMService getFCMService()
{
return FCMClient.getClient(fcmURL).create(IFCMService.class);
}
}