有人知道如何解决此问题吗?我尝试使用从数据库检索到的位置在Map上显示标记。尝试打开应用程序时出错。This is the error message from Logcat
这是我的密码
公共类PassengerMainscreen_Activity扩展了AppCompatActivity实现了OnMapReadyCallback {
boolean isPermissionGranted;
TextView textView;
DatabaseReference firebaseDatabase;
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passenger_mainscreen);
checkpermission();
textView = findViewById(R.id.LocEmpty);
//RETRIEVING LOCATION FROM DATABASE
firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Location");
firebaseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Double lat = Double.valueOf(snapshot.child("latitude").getValue().toString());
Double lng = Double.valueOf(snapshot.child("longitude").getValue().toString());
textView.setText(lat+","+lng);
//DISPLAYING MARKER ON THE MAP
LatLng loc = new LatLng(lat,lng);
googleMap.addMarker(new MarkerOptions()
.position(loc).title("bus"));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
//CHECKING GOOGLE PLAY SERVICES
if (isPermissionGranted) {
if (checkGooglePlayServices()) {
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
getSupportFragmentManager().beginTransaction().add(R.id.CONTAINER, supportMapFragment).commit();
supportMapFragment.getMapAsync(this);
} else {
Toast.makeText(this, "Google Play Services Not available", Toast.LENGTH_SHORT).show();
}
}
}
private boolean checkGooglePlayServices() {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int result = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (result == ConnectionResult.SUCCESS) {
return true;
} else if (googleApiAvailability.isUserResolvableError(result)) {
Dialog dialog = googleApiAvailability.getErrorDialog(this, result, 201, new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(PassengerMainscreen_Activity.this, "User Cancelled Dialog", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
return false;
}
private void checkpermission() {
Dexter.withContext(this).withPermission(Manifest.permission.ACCESS_FINE_LOCATION).withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
isPermissionGranted = true;
Toast.makeText(PassengerMainscreen_Activity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), "");
intent.setData(uri);
startActivity(intent);
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check(); //PERMISSION END
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
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;
}
googleMap.setMyLocationEnabled(true);
}
}
我在编写以下代码后遇到了错误://DISPLAYING MARKER ON THE MAP LatLng loc = new LatLng(lat,lng); googleMap.addMarker(new MarkerOptions() .position(loc).title("bus"));
1条答案
按热度按时间v2g6jxz61#
在这一行代码中,您获得空指针异常
googleMap.addMarker(new MarkerOptions().position(loc).title("bus"));
的原因是您刚刚声明了变量googleMap
,但尚未初始化,因此您可以在onMapReady
方法中初始化变量googleMap
,因为您正在参数中获取像这样
但是要确保你只在
googleMap
变量初始化后才使用它,所以你必须把firebase的代码移到单独的函数中,并在googleMap
初始化后调用它