AlertDialog在MapsActivity中扩展FragmentActivity显示空白消息

问题描述 投票:0回答:1

我想在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();

    }
}

我认为它与FragmentActivity有关,因为在ActivityCompat的警报对话框中可以正常工作。

android android-alertdialog android-fragmentactivity
1个回答
0
投票

尝试更改

MainActivity扩展FragmentActivity

成为

MainActivity扩展了AppCompatActivity

© www.soinside.com 2019 - 2024. All rights reserved.