暂停(异常IlegalStateException)黑莓6.0

时间:2013-04-19 12:49:23

标签: blackberry java-me blackberry-jde

以下是在调试时显示上述异常的代码:

首先,我尝试从下面的菜单项调用类HTTPConnection。

protected MenuItem _SelectDealerItem = new MenuItem("Select Dealer",100,10)
{
    public void run()
    {
        new HTTPConnection();
    }
};

在HTTPConnection类中,我正在检查连接类型并调用另一个类TSelectDealerScreen:

public class HTTPConnection {

ConnectionFactory _factory = new ConnectionFactory();

public HTTPConnection()
{
    int[] _intTransports = {
            TransportInfo.TRANSPORT_TCP_WIFI,
            TransportInfo.TRANSPORT_WAP2,
            TransportInfo.TRANSPORT_TCP_CELLULAR
    };

    for(int i=0;i<_intTransports.length;i++)
    {
        int transport = _intTransports[i];
        if(!TransportInfo.isTransportTypeAvailable(transport)||!TransportInfo.hasSufficientCoverage(transport))
        {
            Arrays.removeAt(_intTransports,i);
        }
    }

    TcpCellularOptions tcpOptions = new TcpCellularOptions();

    if(!TcpCellularOptions.isDefaultAPNSet())
    {
        tcpOptions.setApn("My APN");
        tcpOptions.setTunnelAuthUsername("user");
        tcpOptions.setTunnelAuthPassword("password");
    }

    if(_intTransports.length>0)
    {
        _factory.setPreferredTransportTypes(_intTransports);
    }

    _factory.setTransportTypeOptions(TransportInfo.TRANSPORT_TCP_CELLULAR, tcpOptions);
    _factory.setAttemptsLimit(5);

    Thread t = new Thread(new Runnable()
    {
        public void run()
        {
            ConnectionDescriptor cd = _factory.getConnection("http://excellentrealtors.info/Smart-Trace/get_dealer.php");
            if(cd!=null)
            {
                Connection c = cd.getConnection();
                displayContent(c);
            }
        }
    });
    t.start();
}

private void displayContent(final Connection conn)
{
    UiApplication.getUiApplication().pushScreen(new TSelectDealerScreen(conn));
}
}

在TSelectDealerScreen类中,我只是尝试读取流,但每当我尝试调试时它都会显示非法状态异常,我对黑莓编程并不熟悉,请提出建议。

public class TSelectDealerScreen extends MainScreen
{
RichTextField _rtfOutput = new RichTextField();

public TSelectDealerScreen(Connection conn) 
{   
    _rtfOutput.setText("Retrieving Data.Please Wait");
    add(_rtfOutput);
    ContentReaderThread t = new ContentReaderThread(conn);
    t.start();
}

private final class ContentReaderThread extends Thread {

    private Connection _connection;

    ContentReaderThread(Connection conn)
    {
        _connection = conn;
    }

    public void run()
    {
        String result = "";
        OutputStream os = null;
        InputStream is = null;

        try
        {
            OutputConnection outputConn = (OutputConnection)_connection;
            os = outputConn.openOutputStream();
            String getCommand = "GET " + "/" + " HTTP/1.0\r\n\r\n";
            os.write(getCommand.getBytes());
            os.flush();

            // Get InputConnection and read the server's response
            InputConnection inputConn = (InputConnection) _connection;
            is = inputConn.openInputStream();
            byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
            result = new String(data, "US-ASCII");
            // is.close();
            System.out.print(result);
        }
        catch(Exception e)
        {
            result = "ERROR fetching content: " + e.toString();
        }
        finally
        {
            // Close OutputStream
            if(os != null)
            {
                try
                {
                    os.close();
                }
                catch(IOException e)
                {
                }
            }

            // Close InputStream
            if(is != null)
            {
                try
                {
                    is.close();
                }
                catch(IOException e)
                {
                }
            }
            // Close Connection
            try
            {
                _connection.close();
            }
            catch(IOException ioe)
            {
            }
        }
              // Show the response received from the web server, or an error message
        showContents(result);
    }      
}

public void showContents(final String result)
{
    UiApplication.getUiApplication().invokeLater(new Runnable()
    {
        public void run()
        {
            _rtfOutput.setText(result);
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

HTTPConnection课程中,您可以:

Thread t =  new Thread(new Runnable()
{
    public void run()
    {
        ConnectionDescriptor cd =   _factory.getConnection("http://excellentrealtors.info/Smart-Trace/get_dealer.php");
        if(cd!=null)
        {
            Connection c    =   cd.getConnection();
            displayContent(c);
        }
    }
});
t.start();

在后台线程上运行run()内的所有内容。但是,在displayContent(c)内,您可以:

UiApplication.getUiApplication().pushScreen(new TSelectDealerScreen(conn));

这是一个UI操作。

尝试从后台线程修改UI通常会导致IllegalStateException

我相信你只需要用这个来调用pushScreen()

private void displayContent(final Connection conn) {

    final UiApplication app = UiApplication.getUiApplication();
    app.invokeLater(new Runnable() {
       public void run() {
          // this code is run on the UI thread
          app.pushScreen(new TSelectDealerScreen(conn));
       }
    });
}

此外,如果您是Android开发人员,并且希望获得有关正常背景/ UI线程内容的帮助,您可以check out this other answer I wrote on "porting" AsyncTask to BlackBerry Java