Msgstr "pushModalScreen called by the non-event thread" thrown onto the event thread

I am trying to get my Blackberry application to display a custom modal dialog and open the initial thread until the user closes the dialog.

final Screen dialog = new FullScreen();

...// Fields are added to dialog

Application.getApplication().invokeAndWait(new Runnable()
{

    public void run()
    {
        Application.getUiApplication().pushModalScreen(dialog);             
    }
});

This throws an exception saying: "pushModalScreen triggered by a non-event stream" even though I use invokeAndWait to call pushModalScreen from the event stream.

Any ideas on what the real problem is?

Here is the code to duplicate this problem:

package com.test;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class Application extends UiApplication {
    public static void main(String[] args)
    {
        new Application();
    }

    private Application()
    {
        new Thread()
        {
            public void run()
            {
                Application.this.enterEventDispatcher();
            }
        }.start();

        final Screen dialog = new FullScreen();
        final ButtonField closeButton = new ButtonField("Close Dialog");
        closeButton.setChangeListener(new FieldChangeListener()
        {

            public void fieldChanged(Field field, int context)
            {
                Application.getUiApplication().popScreen(dialog);
            }
        });
        dialog.add(closeButton); 
        Application.getApplication().invokeAndWait(new Runnable()
        {

            public void run()
            {
                try
                {
                    Application.getUiApplication().pushModalScreen(dialog);
                }
                catch (Exception e)
                {
                    // To see the Exception in the debugger
                    throw new RuntimeException(e.getMessage());
                }
            }
        });

        System.exit(0);
    }
}

I am using the component package version 4.5.0.

+3
source share
3 answers

, Exception invokeLater invokeAndWait, , invokeAndWait invokeLater Java:

public static void invokeAndWait(final Application application,
    final Runnable runnable)
{
    final Object syncEvent = new Object();
    synchronized(syncEvent)
    {
        application.invokeLater(new Runnable()
        {

            public void run()
            {   
                runnable.run();
                synchronized(syncEvent)
                {
                    syncEvent.notify();
                }
            }
        });
        try
        {
            syncEvent.wait();
        }
        catch (InterruptedException e)
        {
            // This should not happen
            throw new RuntimeException(e.getMessage());
        }
    }
}

, invokeAndWait , .

+2

, , .

public class Application extends UiApplication {
    public static void main(String[] args)
    {
        new Application().enterEventDispatcher();
    }

private Application()
{
    final Screen dialog = new FullScreen();
    final ButtonField closeButton = new ButtonField("Close Dialog");
    closeButton.setChangeListener(new FieldChangeListener()
    {
        public void fieldChanged(Field field, int context)
        {
            Application.getUiApplication().popScreen(dialog);
        }
    });
    dialog.add(closeButton); 

    // this call will block the current event thread
    pushModalScreen(dialog);

    System.exit(0);
   }
}
+2

Use this:

UiApplication.getUiApplication().invokeAndWait(new Runnable() {
    public void run() {
        pushScreen(new MyScreen());
    }
});
0
source

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


All Articles