Alternative Methods in codenameone

[亡魂溺海] 提交于 2019-12-10 11:28:49

问题


I have been designing an application using CodeNameOne via Netbeans and have encountered some issues, notably the "cannot find symbol" compile-error. After further research, I discovered that it was due to CNO not supporting certain libraries/misc.

I was wondering if there were alternate ways to use the following libraries/perform tasks:

(Read from file) import java.io.File; symbol: class File location: package java.io

(Throw FileNotFoundException) import java.io.FileNotFoundException; symbol: class FileNotFoundException location: package java.io

(Read data) import java.util.Scanner; symbol: class Scanner location: package java.util

(Math-based functions) symbol: method pow(double,int) location: class Math

(Create formatted Strings) symbol: method format(String) location: class String


回答1:


I can't say for sure about all the examples you provided but you should consider looking into the CN1 API Javadocs for information on how to do it "their way".

For example, when it comes to File, the Javadocs say:

The main reason java.io.File & java.io.FileInputStream weren't supported directly has a lot to do with the richness of those two API's. They effectively allow saving a file anywhere, however mobile devices are far more restrictive and don't allow apps to see/modify files that are owned by other apps.

So, the answer to that one is, use theirs. Similarly, search the CN1 Javadocs to learn the appropriate ways to do what you need using their APIs.

You can also refer to the developer guide e.g. most file based operations should be performed either thru Storage or FileSystemStorage both of which are covered here. You should be very careful with code that uses java.io.File as this might rely on desktop specific behaviors.

FileNotFoundException can be replaced with IOException (it's a subclass of IOException anyway).

There is no direct equivalent to Scanner but there are builtin parsers for JSON, XML, CSV & Properties.

Some of the math functions are in MathUtil that includes Math.pow(double, double).

There is no equivalent to the format() method of Java SE. You would need to concatenate strings instead.

Notice that if you post some code as a question we can sometimes help with the Codename One equivalent.



来源:https://stackoverflow.com/questions/36681724/alternative-methods-in-codenameone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!