0

我正在尝试使用 field2.BrowserField 实现简单的用户交互:单击按钮时 BrowserField 会加载另一个页面,但我得到了 IllegalStateException。

这是我的代码:

public class BrowserScreen extends MainScreen {
  private BrowserField browser;

  public BrowserScreen() {
      super();
      setTitle("Browser State example");

      ButtonField btn1 = new ButtonField("test1");
      btn1.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            test1();                
        }
      });
      add(btn1);
      ButtonField btn2 = new ButtonField("test2");
      btn2.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            test2();                
        }
      });
      add(btn2);

      browser = new BrowserField();
      add(browser);
      browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
  }

  private void test1() {
      UiApplication.getUiApplication().invokeLater(new Runnable() {         
        public void run() {             
            browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
        }
    });

  }

  private void test2() {
      synchronized (Application.getEventLock())
        {
         browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
        }
  }

}

4

2 回答 2

0

试试这个:

public class BrowserScreen extends MainScreen {
  private BrowserField browser;

  public BrowserScreen() {
      super();
      setTitle("Browser State example");

  ButtonField btn1 = new ButtonField("test1");
  btn1.setChangeListener(new FieldChangeListener() {

    public void fieldChanged(Field field, int context) {
        //test1();
        test1("http://www.blackberry.com/developers");
    }
  });
  add(btn1);
  ButtonField btn2 = new ButtonField("test2");
  btn2.setChangeListener(new FieldChangeListener() {

    public void fieldChanged(Field field, int context) {
        //test2();
        test1("http://www.blackberry.com/developers");
    }
  });
  add(btn2);

  browser = new BrowserField();
  add(browser);

  //browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
  test1("http://stackoverflow.com/");
}

  private void test1(final String url) {
      UiApplication.getUiApplication().invokeLater(new Runnable() {         
        public void run() {     
            //browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
            browser.requestContent(url + ";deviceside=true");
        }
    });

  }

//  private void test2() {
//      synchronized (Application.getEventLock())
//        {
//         browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
//        }
//  }
}
于 2011-03-13T20:35:55.580 回答
0

哇,添加 browser.setFocus(); 做把戏

public class BrowserScreen extends MainScreen {
  private BrowserField browser;

  public BrowserScreen() {
      super();
      setTitle("Browser State example");

      ButtonField btn1 = new ButtonField("test1", ButtonField.CONSUME_CLICK);
      btn1.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            test1();                
        }
      });
      add(btn1);

      browser = new BrowserField();
      add(browser);
      browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
  }

  private void test1() {
      UiApplication.getUiApplication().invokeLater(new Runnable() {         
        public void run() {      
            browser.setFocus();
            browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
        }
    });

  }

}

于 2011-03-14T14:59:19.537 回答