“pushModalScreen called by a non-event thread” thrown on event thread

后端 未结 3 1339
梦毁少年i
梦毁少年i 2020-12-21 08:53

I am trying to get my Blackberry application to display a custom modal dialog, and have the opening thread wait until the user closes the dialog screen.

fina         


        
3条回答
  •  独厮守ぢ
    2020-12-21 09:28

    Building on Max Gontar's observation that the Exception is not thrown when using invokeLater instead of invokeAndWait, the full solution is to implement invokeAndWait correctly out of invokeLater and Java's synchronization methods:

    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());
            }
        }
    }
    

    Unfortunately, the invokeAndWait method cannot be overridden, so care must be used to call this static version instead.

提交回复
热议问题