Android 未显示AlertDialog

Android 未显示AlertDialog,android,android-studio,android-alertdialog,Android,Android Studio,Android Alertdialog,这是我为显示警报对话框而编写的代码,但它仍然没有显示 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("GPS not Enabled"); builder.setMessage("Please enable the GPS for proper functioning

这是我为显示警报对话框而编写的代码,但它仍然没有显示

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("GPS not Enabled");
                builder.setMessage("Please enable the GPS for proper functioning of the app");
                builder.setNeutralButton("Go To Settings", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                });
                builder.setCancelable(false);

                AlertDialog showDialog = builder.create();
                showDialog.show();

请帮忙。我真的无法理解原因。

警报对话代码正常。如果您正在谈论警报对话框出现后的响应。然后将空档换为正极。看看我发布的代码


public class MainActivity extends AppCompatActivity {

    Button alertButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alertButton = findViewById(R.id.alertBtn);

        alertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAlertButton();

            }
        });
    }

    public void setAlertButton(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Fire alert ")
                .setPositiveButton("Fire", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        
        AlertDialog showDialog = builder.create();
        showDialog.show();

    }

}

你有错误吗?