Alert Dialog in non activity class

前端 未结 4 2057
南旧
南旧 2021-01-26 02:37

I have a code which checks some data and shows an alert in a non activity class. But while running application program crashed and does not showing alert dialog. I used below

4条回答
  •  野的像风
    2021-01-26 03:32

    When you create the Non Activity class in your Activity, pass the context to its constructor:

    NonActivityClass nonActivityClass = new  NonActivityClass(this);
    

    Non Activity constructor:

    Context mContext;
    public NonActivityClass(Context context){
        mContext = context;
    }
    

    then use:

    new AlertDialog.Builder(mContext)... in the Non Activity class

提交回复
热议问题