android - AlertDialog 在扩展 FragmentActivity 的 MapsActivity 中显示空白消息
问题描述
我想在 MapReady 上显示 AlertDialogue。我在其他课程中实现了警报对话,它工作正常。
当我在扩展 FragmentActivity 的 MapsActivity 中添加警报对话代码时,它无法按预期工作。
当我运行 Activity 时,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();
}
}
我认为这与 FragmentActivity 有关,因为在 ActivityCompat 的警报对话中工作得很好。
解决方案
尝试改变
MainActivity 扩展了 FragmentActivity
成为
MainActivity 扩展了 AppCompatActivity
推荐阅读
- python-3.7 - 如何从进一步计算中排除列表的某些元素
- html - list-style-image: url('imageurl') 不在网页上显示图片
- discord.js - Discord.js 事件更新存在垃圾邮件
- python - 初学者python,TypeError:+的不支持的操作数类型:'float'和'str'
- php - 为什么会出现“警告:文件(...):无法打开流”错误?
- api - UFT API 测试:如何为请求中的元素生成数字
- scala - 窗口规格/功能执行最佳方式或应首选任何替代方式
- python - Python 文件执行失败
- java - Spring-MVC / Java的汉字编码问题
- jquery - 如何在子跨度中切换课程?