如何在 class 中显示扩展应用程序的 AlertDialog

How to show an AlertDialog in class which extends Application

我有一个扩展应用程序的 class。 目标是在应用程序开始后立即显示一个 AlertDialog(带有 1 个 OK 按钮),并且仅在那时(每次用户运行该应用程序时,但 而不是 每次用户进入来自其他活动的主要 Activity。

主要Activity的名字是Activity1.

所以这里是扩展应用程序的 class:

   public class MyApp extends Application {
        @Override
        public void onCreate() {
            super.onCreate();

            Activity1 myActivity1=new Activity1();

            AlertDialog.Builder builder = new AlertDialog.Builder(myActivity1);
             builder.setMessage("Hello").setCancelable(false).setPositiveButton("OK",                                                                   new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    //do things
               }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }

这是我得到的错误:

java.lang.RuntimeException: Unable to create application…...NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

我想 activity1 不是正确的论点。我也尝试了 "this" 参数。我的意思是:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

也没有用。

如果有人对此有任何想法,我将不胜感激。谢谢!

您需要 class 扩展具有布尔值的应用程序。这用于存储只要应用程序持续存在的全局值 运行.

public class MyApplication extends Application {
    public boolean hasShownAlertDialog = false; // you can do private and use getter/setters, but, that's frowned upon.
}

在您的 android 清单中,在应用程序下,您需要定义您正在使用自定义实现。

<application android:name="com.example.MyApplication" [...]>

在您的 Activity1 中,在 onCreate 中,您检查全局变量:

public void OnCreate(Bundle bundle) {
    ...
    if (!(MyApplication)getApplication().hasShownAlertDialog) {
        // show alert dialog
        (MyApplication)getApplication().hasShownAlertDialog = true;
    }
}

使用全局变量确定您是否已经显示对话框。你的 MyApp 看起来像这样

    public class MyApp extends Application {

    // showDialog will be true for the first time
    private boolean showDialog = true;

    public boolean getDialog() {
        return showDialog;
    }

    public void setDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }

    @Override
    public void onCreate() {
        super.onCreate();



    }
}

然后在 Activity1 的 onCreate() 方法中添加以下内容

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

    boolean showDialog = ((MyApp) this.getApplication()).getDialog();
    if(showDialog == true) {
        //Toast.makeText(this, "Show dialog", Toast.LENGTH_LONG).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Hello").setCancelable(false).setPositiveButton("OK",                                                                   new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //do things
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
        // Set showDialog to false now so you don't see it again
        ((MyApp) this.getApplication()).setDialog(false);
    }
    else {
        Log.d("DIALOG", "Do not show dialog");
    }

}

不要忘记在清单文件中声明您的应用程序 class。在 <application> 标签内添加 android:name=".MyApp"。例如:

<application
    android:name=".MyApp"
    android:icon=""
    ....
>