The runOnUiThread (Runnable) method in type Activity is not applicable for arguments (void)

I am trying to create a dialog from a non-UI thread in onUtteranceCompleted ():

runOnUiThread( new Thread(new Runnable() { public void run() { MyDialog.Prompt(this); } }).start()); 

Request () is a simple static method of the MyDialog class:

  static public void Prompt(Activity activity) { MyDialog myDialog = new MyDialog(); myDialog.showAlert("Alert", activity); } 

The problem is that I bought two errors for what I am trying to do:

  • The runOnUiThread (Runnable) method in type Activity is not applicable for arguments (void)
  • The Prompt (Activity) method in type MyDialog is not applicable for arguments (new Runnable () {})

All I wanted was to "do it right" by delaying the creation of the dialog box until the user interface flow, but it seems that I am missing something fundamental.

What am I missing and how can I accomplish the seemingly simple task that I am trying to achieve?

+4
source share
1 answer

It should be:

 runOnUiThread(new Runnable() { public void run() { MyDialog.Prompt(NameOfYourActivity.this); } }); 

It says that this is not applicable for arguments (void), because you are trying to start a thread using the start method (which is the void method). runOnUiThread gets the running object, and you do not need to worry about its launch, the OS is running for you.

As for the second error, this is because in this area this refers to the Runnable object that you are initializing, and not to the activity reference. So, you must explicitly indicate which this you are referring to (in this case, YourActivityName.this ).

+11
source

Source: https://habr.com/ru/post/1345977/


All Articles