-1

This question is very similar to questions that have been asked in the past but please bear with me as it is still a unique question. Basically, I have a class that gets application permissions, and if the user does not have internet running, then when the auto login screen comes, it is stuck in loading. So what I want to do is show a dialog message, and the user will click ok to close the app. For dialogs, I need the context, and I must run on the main thread. I have posted an image of the code because I want to show you that runOnUIThread is red. Here is the error I get form Android Studio

Cannot resolve method runOnUIThread.

Here is what I had

enter image description here

Problem: For some reason, runOnUIThread is not usable. Does anyone have a counter proposal, or a reason as to why I am having this problem?

Here is the code:

public void alert() {

    new Thread()
    {
        public void run()
        {
            application.runOnUiThread(new Runnable()  // application is the context of my current activity.
            {
                public void run() //I display my alert Dialog here.
                {
                    AlertDialog build= new AlertDialog.Builder(application.getApplicationContext())
                            .setTitle("Error")
                            .setMessage("Sorry there seems to be a problem with the service. Please check to make sure you have a stable internet connection. ")
                            .setPositiveButton("Ok, I understand.", (dialog, which) -> System.exit(0))
                            .show();
                    build.setCancelable(false);
                    Button positive= build.getButton(DialogInterface.BUTTON_POSITIVE);
                    positive.setTextColor(application.getResources().getColor(R.color.buttonPrimaryColor));
                    positive.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                }
            });
        }
    }.start();

Here is how I have made it work with a Toast in the past.

public void shortToast(String msg) {
    Observable.just(msg)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(message -> {
                Toast.makeText(application, message, Toast.LENGTH_SHORT).show();
            });
}

// In the main method 

shortToast("Sorry an error occured");
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Naman Jain
  • 321
  • 5
  • 21

2 Answers2

2

For some reason, runOnUIThread is not usable

The method that you are trying to invoke is runOnUiThread(). That is a method on Activity. Whatever application is, it is not an Activity.

Does anyone have a counter proposal

Move this code into an Activity. Generally, pop-ups (dialogs, snackbars, etc.) should be displayed by an Activity. And only an Activity can show a Dialog, such as an AlertDialog.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Very nice explanation. So I have changed it to RegisterInfoActivity.runOnUiThread(new Runnable(). – Naman Jain Mar 02 '18 at 17:19
  • But for some reason runOnUiThread is now showing me a different error. Is there any way for me to use alertDialog outside the activity? I have been successful with toast messages. – Naman Jain Mar 02 '18 at 17:22
  • @NamanJain What error? I suspect something like "cannot call method in a static context." Is that right? – Code-Apprentice Mar 02 '18 at 17:23
  • @NamanJain: `RegisterInfoActivity` is a Java class. `runOnUiThread()` is not a `static` method; you do not call it on the class. "Is there any way for me to use alertDialog outside the activity?" -- not realistically. I would expect your code to crash when you try creating the dialog; `Dialog` can only be created by an `Activity`, not an arbitrary `Context`. – CommonsWare Mar 02 '18 at 17:26
  • @NamanJain: Move the code that displays the `Dialog` into an `Activity`, as I indicated in my answer. – CommonsWare Mar 02 '18 at 17:31
  • No. What I mean, is there a way for me to pass my alert dialog in this class. Allow me to show you a successful toast that I have been able to create. Look at my edit. – Naman Jain Mar 02 '18 at 17:32
  • @NamanJain: Things other than activities can show a `Toast`, raise a `Notification`, or start an `Activity`. A `Dialog` is not a `Toast`, `Notification`, or `Activity`. Move the code that displays the `Dialog` into an `Activity`. Or, do not use a `Dialog`, but instead use a `Toast`, `Notification`, or `Activity`. – CommonsWare Mar 02 '18 at 17:36
  • Alright! Now were talking. I appreciate your answer very much. Just one more inquiry before I conclude my findings. What allows toast and notifications to work without activities? I want to implement that. Its okay though if you dont know this. Thanks for everything. – Naman Jain Mar 02 '18 at 17:38
  • @NamanJain: "What allows toast and notifications to work without activities?" -- notifications are not displayed by your process, let alone by an activity. Notifications are displayed by a system process. You are simply telling the system process "hey, please show a `Notification` that looks like...". As to why `Toast` is more flexible, you would have to ask Google. – CommonsWare Mar 02 '18 at 17:40
  • Lol. True. Alright thanks a lot man. I have decided to create a new activity, and launch it from my non-activity. – Naman Jain Mar 02 '18 at 17:44
0

Try to use an activity to run on UI, not application