首页 > 解决方案 > 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 的警报对话中工作得很好。

标签: androidandroid-alertdialogandroid-fragmentactivity

解决方案


尝试改变

MainActivity 扩展了 FragmentActivity

成为

MainActivity 扩展了 AppCompatActivity


推荐阅读