【问题标题】:how to set the gravity in toast and can i change the position of my alert dialog?如何在 toast 中设置重力,我可以更改警报对话框的位置吗?
【发布时间】:2021-03-27 16:23:57
【问题描述】:

我想将 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$3.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();
}

【问题讨论】:

  • 请发布堆栈跟踪
  • 设置重力不返回它被调用的吐司(developer.android.com/reference/android/widget/…。它返回 void。
  • 在创建吐司后直接输入分号,然后在吐司上调用 setGravity,然后调用 show
  • 您正在尝试将 void 设置为您的 toast 变量。我想知道它是如何编译的
  • 等等,让我告诉你错误

标签: java android toast android-toast


【解决方案1】:

用途:

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();     
}

【讨论】:

  • 我也试过了,但问题仍然存在
  • 谢谢亲爱的它现在可以工作了你能告诉我我也可以改变我的对话框的重力吗?
猜你喜欢
  • 2012-09-15
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多