I’m trying to make an embedded web page on my blackberry app, and I'm having trouble setting the starting URL.
I can only set the URL when I do it from a callback from a TextField
. When I try to do it after I push the new screen on, the app no longer works (it does nothing when you try to run it.
The code that works is the 开发者_如何转开发following:
protected boolean keyChar(char key, int status, int time)
{
if ( key == Characters.ENTER )
{
Application.getApplication().invokeLater( new Runnable()
{
public void run() {
//progressBar.reset("", 0, 100, 0);
browserField.requestContent(locationBar.getText().trim());
}
});
return true;
}
return super.keyChar(key, status, time);
}
When I try to set it automatically after everything is created, nothing happens when I run the app.
Code snippet
public MyApp()
{
pushScreen(new BrowserFieldScreen());
// if I have this here, the app will not open
Application.getApplication().invokeLater( new Runnable()
{
public void run() {
gBrowserField.requestContent("http://google.com");
}
});
}
The complete program:
package mypackage;
import java.util.Enumeration;
import java.util.Hashtable;
import net.rim.device.api.browser.field.ContentReadEvent;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldListener;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import org.w3c.dom.Document;
/**
* BrowserField2ProgressTracker
* - A BrowserField2 Mini-browser application that keeps track of page-loading progress
* and displays progress information to the user.
*/
public class MyApp extends UiApplication {
static BrowserField gBrowserField;
public static void main(String[] args){
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp() {
pushScreen(new BrowserFieldScreen());
/*
if I have this hear, the ap will not open
Application.getApplication().invokeLater( new Runnable() {
public void run() {
gBrowserField.requestContent("http://google.com");
}
});
}
*/
}
/**
* BrowserFieldScreen
* - A screen that contains a location bar, a progress bar, and a browser field
*/
class BrowserFieldScreen extends MainScreen {
// The location bar where URL will be typed in
private TextField locationBar;
// The BrowserField
private BrowserField browserField;
public BrowserFieldScreen() {
// progressTracker = new BrowserFieldLoadProgressTracker(10f);
createGUI();
}
private void createGUI() {
VerticalFieldManager mainManager = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLLBAR | Manager.HORIZONTAL_SCROLLBAR );
locationBar = new TextField() {
protected boolean keyChar(char key, int status, int time) {
if ( key == Characters.ENTER ) {
Application.getApplication().invokeLater( new Runnable() {
public void run() {
//progressBar.reset("", 0, 100, 0);
browserField.requestContent(locationBar.getText().trim());
}
});
return true;
}
return super.keyChar(key, status, time);
}
};
locationBar.setBackground(BackgroundFactory.createSolidBackground(Color.BEIGE));
locationBar.setText("http://google.com");
browserField = new BrowserField();
MyApp.gBrowserField=browserField;
mainManager.add(locationBar);
mainManager.add(browserField);
add(mainManager);
}
}
I don't think you can call invokeLater()
until your app, theApp
has enter the main event thread. Which is done by calling theApp.enterEventDispatcher()
instead try changing your BrowserFieldScreen
constructor to:
public BrowserFieldScreen() {
// progressTracker = new BrowserFieldLoadProgressTracker(10f);
createGUI();
browserField.requestContent("http://google.com");
}
精彩评论