问题
I wrote some code to connect to a URL in a BrowserField. In the process its shows Illegal State Exception. If I click Continue, then it connects. I do not understand what is the problem.
Here is the code I am using:
class MoreBrowserScreen extends MainScreen
{
String email;
public MoreBrowserScreen(String email)
{
this.email=email;
setTitle(HeaderManager.tabsManager());
HeaderManager.more.setFocus();
TabsManager(Paths.HOME,Paths.ALERTS,Paths.COLL,Paths.APP,Paths.FMORE).getTabs();
BrowserField myBrowserField = new BrowserField();
add(myBrowserField);
myBrowserField.requestContent(email);
}
protected void makeMenu(Menu menu, int instance)
{
MenuItemClass mic = new MenuItemClass();
menu.add(mic.getExitItem(0, 0));
}
回答1:
Hi I was having same problem, first you need to use invokelater because you are in event thread, and for some mysterious reason also set focus to browser, here is what solved my problem:
private void buttonClicked() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.setFocus();
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
回答2:
I have been pulling my hair on this one too. I tried the browser.setFocus() and it worked partially. Then I realise that there is another problem. Some websites does an automatic redirect that causes the BrowserField to refresh and produce the error again. So here is the solution that will solve problem once and for all. First declare the browserField as a class member.
private BrowserField browserField;
Then when you instantiate your BrowserField, do the following.
browserField = new BrowserField();
ProtocolController eventsProtocolController = new ProtocolController(browserField) {
public void handleNavigationRequest(BrowserFieldRequest request) throws Exception {
browserField.setFocus();
super.handleNavigationRequest(request);
}
};
browserField.getConfig().setProperty(BrowserFieldConfig.CONTROLLER, eventsProtocolController);
It seems that this problem occurs when there are other fields on your screen that can "steal" away the focus. So the above code will ensure that whatever navigation will force the focus back to the BrowserField. In my opinion it is a problem with BrowserField, but here is my work around.
sigh! Seems after testing, this work-around works only for OS 6.. but not OS 5. I have not found any solution for OS 5 yet.
来源:https://stackoverflow.com/questions/5318560/illegal-state-exception-when-connecting-to-url-in-browserfield