简体   繁体   中英

Alert Dialog in non activity class

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 code...

if( str.isEmpty() || strPort.isEmpty()){
        new AlertDialog.Builder(Mtx.getContext())
                    .setMessage("Please provide a valid String")
        .setTitle("HAT Alert !!!")
        .setCancelable(true)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton){
                return;
            }
        }).show();

code for Mtx.getContent() method

public class Mtx extends Application {
public static Context context;

@Override
public void onCreate(){
   super.onCreate();
   Mtx.context = getApplicationContext();
}

public static Context getContext(){
       return Mtx.context;
}
}

error message showing in log cat

10-15 12:50:33.708: E/global(329): Deprecated Thread methods are not supported.
10-15 12:50:33.708: E/global(329): java.lang.UnsupportedOperationException
10-15 12:50:33.708: E/global(329):  at java.lang.VMThread.stop(VMThread.java:85)
10-15 12:50:33.708: E/global(329):  at java.lang.Thread.stop(Thread.java:1280)
10-15 12:50:33.708: E/global(329):  at java.lang.Thread.stop(Thread.java:1247)
10-15 12:50:33.708: E/global(329):  at mediatronix.HAT.Splash$1.run(Splash.java:36)

please correct me

thanks

Are you sure, the dialog is the problem? What is at line 36 of your Splash.java file? Because according to the stacktrace, there's the problem.

You simply cannot show an AlertDialog using application's context. Dialogs are supposed to attached to an activity's window using its context and stay alive inside its life-cycle. Therefore, you simply need to provide an activity's context which will then allow you to show a Dialog.

As an alternate, I would suggest you to show an Activity themed as dialog using android:theme="@android:style/Theme.Dialog" in your AndroidManifest.xml . This will serve the purpose of manipulating a dialog but in real it will be an activity. As an advantage, you can launch your activities anywhere in your code as long as you have access to application's context.

For more info, read this answer.

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

My guess is that your context is null...

Can't you send down a context, to the "codes which checks some data" before starting to checking the data, instead of trying to fetch a context that doesn't exists?

Else make a activity that makes the alertdialog instead, and then startup that activity from your "service" class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM