我想在MapReady上显示AlertDialogue。我在其他班级实施了警报对话框,并且运行良好。
当我在扩展Fragmentactivity的Mapsactivity中添加警报对话代码时,它无法按预期工作。
当我运行活动时,AlertDialogue确实显示带有按钮。但是标题和消息不显示
public class Mapsactivity extends Fragmentactivity implements GoogleMap.OnCameraIdleListener,OnmapReadyCallback {
private GoogleMap mMap;
private Location currentLocation;
private FusedLocationProviderClient fusedLocationProviderClient;
private static final int LOCATION_REQUEST_CODE =101;
private static final String Log_TAG = "Maps_Log_Tag_Results";
private Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (activityCompat.checkSelfPermission(Mapsactivity.this,android.Manifest.permission.accESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && activityCompat.checkSelfPermission(Mapsactivity.this,android.Manifest.permission.accESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
activityCompat.requestPermissions(this,new String[] {android.Manifest.permission.accESS_FINE_LOCATION},LOCATION_REQUEST_CODE);
return;
}
fetchLastLocation();
}
private void fetchLastLocation(){
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;
SupportMapFragment supportMapFragment= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
supportMapFragment.getMapAsync(Mapsactivity.this);
}else{
Toast.makeText(Mapsactivity.this,"No Location recorded",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onmapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnmyLocationButtonClicklistener(onmyLocationButtonClicklistener);
mMap.setOnmyLocationClicklistener(onmyLocationClicklistener);
mMap.getUiSettings().setzoomControlsEnabled(true);
mMap.getUiSettings().setzoomGesturesEnabled(true);
mMap.setMyLocationEnabled(true);
mMap.setOnCameraIdleListener(this);
LatLng latLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngzoom(latLng,13));
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hello");
builder.setMessage("This is alert dialogue");
// add the buttons
builder.setPositiveButton("Awesome",null);
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}