質問

I am a beginner in blackberry app development.

I had installed the blackberry plugin in eclipse successfully. my first app needs to load html files from a folder I had created named "assets" inside my project. so how can I load the html file "assets/index.html"?

public static void main(String[] args)
{
    // Create a new instance of the application and make the currently
    // running thread the application's event dispatch thread.
    Ramadanclass theApp = new Ramadanclass();       
    theApp.enterEventDispatcher();
}

mainscreen:

public final class Ramadan extends MainScreen
{ 

    public Ramadan()
    {                   
        setTitle("Ramadan");    
    }
} 

Edit: what I tried:

BrowserField myBrowserField = new BrowserField(); 
add(myBrowserField); 
myBrowserField.requestContent("assets/index.html"); 

which had this error

no navigation request handler for assets/index.html

役に立ちましたか?

解決

As the link that @Turdnugget supplied says:

The BrowserField class does not access resources using a folder structure. The BrowserField class displays the first resource found that matches the specifed file name.

That means that even if you put index.html into an assets folder, the BlackBerry build process and/or runtime will flatten out (i.e. ignore) your directory structure and therefore you'll have to load the html file with:

myBrowserField.requestContent("local:///index.html");

As you can imagine, this works ok if you only have one file named index.html in your project, but if you have two files with that name, in different folders, the browser field may not load the one you want. I have projects where I do have different versions of html files that are loaded depending on the device, or the version of the app (full vs. free). So, I like to be able to store my html resources in different folders.

To accomplish this, I've used the following code with BrowserField:

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

  InputStream content = getClass().getResourceAsStream("/resourcesWeb/index.html");     
  try {
     byte[] html = IOUtilities.streamToBytes(content);
     browser.displayContent(new String(html), "http://localhost");
  } catch (IOException e) {
     e.printStackTrace();
  }

For this to work, I store the file index.html in a folder named resourcesWeb (name it whatever you like), and that folder is located inside the top-level res folder.

Note that this is not Android. There is not normally an assets directory. When you use the BlackBerry Eclipse plug-in to create a new project, it creates a src and a res folder for you. I would use that. Create a new project if you need to. Then, keep all non-source code resources (html, xml, images, etc.) somewhere under the res folder.

If you didn't create your project this way, you may find that your app is built without including the assets folder you created. You can probably fix this by right clicking on your project in Eclipse, and selecting Properties. You can add the assets folder to the Build Path using this dialog:

enter image description here

and notice that assets should show up in the Order and Export tab as well.

However, I think it's easiest just to create and use projects the way the BlackBerry Eclipse plugin wants to. This will also make your projects more familiar to other BlackBerry Java developers that see your code. In my opinion, you should save Android naming conventions for Android projects.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top