Toast消息未显示

问题描述 投票:2回答:5

我试图使用以下代码在我的应用程序中显示一个Toast消息。

AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Do you want to continue?");
            alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    try{
                        //This code generates an Activity Not Found exception   
                        }
                        catch(ActivityNotFoundException e) {
                            System.out.println("Activity Not Found Exception Raised");
                            Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
                        }
                    }

            });

            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
            alert.show();

但是这条消息仅在少数设备上显示。我已经在HTC One X上测试了这个代码,Android版本4.2.2正在运行。

如果我在Micromax A63上测试同样的代码,它也有Android 4.2.2,但它不适用于它。

我在互联网上搜索了这种错误,他们主要是在设置菜单中告诉应用程序通知禁用选项。但我的应用程序通知未被禁用。

编辑

我在AlertDialog里面做这件事

有人可以帮我解决这个问题。

android android-ui toast android-toast
5个回答
14
投票

如果您还没有想到这一点,请确保您没有禁用相关应用程序的通知;这也禁用了祝酒词。

https://code.google.com/p/android/issues/detail?id=35013


4
投票

如果您在活动中使用它,请使用:

Toast.makeText(ActivityName.this, "My Toast Message", Toast.LENGTH_SHORT).show();

如果您将它用于片段,那么:

Toast.makeText(getActivity, "My Toast Message", Toast.LENGTH_SHORT).show();

或在适配器中

Toast.makeText(context, "My Toast Message", Toast.LENGTH_SHORT).show();

注意:在适配器中,上下文表示您在适配器中声明的上下文。


2
投票

试试这个

Toast.makeText(getBaseContext(), "My Toast Message", Toast.LENGTH_SHORT).show();

要么

Toast.makeText(PreferenceActivity.this, "My Toast Message", Toast.LENGTH_SHORT).show(); `

有关详细信息,请查看THIS

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Do you want to continue?");
            alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    try{
                        //This code generates an Activity Not Found exception   
                        }
                        catch(ActivityNotFoundException e) {
                            System.out.println("Activity Not Found Exception Raised");
                           ShowToast();
                        }
                    }

            });

            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
            alert.show();

}

public void ShowToast()
{
 Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
}

0
投票

上下文背景;

然后打电话并初始化OnCreate()

context=this; (Use in Activity)

context=this.getActivity(); (Use in Fragment)

然后用

Toast.makeText(context, "My Toast Message", Toast.LENGTH_LONG).show();

0
投票

尝试使用getApplicationContext()而不是getBaseContext

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