如何在 toast 中设置重力,我可以更改警报对话框的位置吗?

how to set the gravity in toast and can i change the position of my alert dialog?

我想将 toast 的重力设置在布局的中心,但是当我添加重力和 运行 我的应用程序时,我的应用程序会重新启动,然后崩溃。我已经通过拿东西和不带东西两种方式尝试了吐司 这是我的代码。错误也附上

@Override
public void onBackPressed() {
    new AlertDialog.Builder(MainActivity.this)
            .setTitle("Exit")
            .setIcon(R.drawable.ic_baseline_warning_24)
            .setMessage("Sure? Your Want to Exit.")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishAffinity();
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    }).setNeutralButton("Other", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast toast = Toast.makeText(MainActivity.this, "Click yes for exit.", Toast.LENGTH_SHORT)
                    .setGravity(Gravity.CENTER, 0,0);
                toast.show();

        }
    }).show();

错误是从 运行 选项卡中复制的

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.androiddevelopment, PID: 3210
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Toast.setGravity(int, int, int)' on a null object reference
    at com.example.androiddevelopment.MainActivity.onClick(MainActivity.java:52)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我也试过了,但问题没有解决

@Override
public void onBackPressed() {
    new AlertDialog.Builder(MainActivity.this)
            .setTitle("Exit")
            .setIcon(R.drawable.ic_baseline_warning_24)
            .setMessage("Are you sure you want to exit")
            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishAffinity();

                }
            }).setNeutralButton("Help", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast toast = Toast.makeText(MainActivity.this, "This is an \"Android Studio\" learning app", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0,0);
            toast.show();


        }
    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    }).show();
}

使用:

public void onClick(DialogInterface dialog, int which) {
     Toast toast = Toast.makeText(MainActivityJava.this, "Click yes for exit.", Toast.LENGTH_SHORT);
     toast.setGravity(Gravity.CENTER, 0,0);
     toast.show();     
}