我正在尝试将一个Android应用转换为codenameone应用,然后再转换为iOS应用。现有的Android应用使用android.utils.Log,它使用两个String变量,而com.codename1.io.Log使用一个String变量和一个int作为日志级别。我没有修改现有代码,而是尝试围绕com.codename1.io.Log编写一个 Package 类W_Log:
import com.codename1.io.Log;
import java.io.IOException;
public class W_Log {
static Log w_Log;
public W_Log() {
w_Log = Log.getInstance();
}
public void d(String a, String b) {
w_Log.p(a + b, Log.DEBUG);
}
public void e(String a, String b) {
w_Log.p(a + b, Log.ERROR);
}
public void e(String a, String b, IOException e) {
w_Log.p(a + b, Log.ERROR);
w_Log.e(e);
}
public void i(String a, String b) {
w_Log.p(a + b, Log.INFO);
}
}
在我现有的代码中,我注解掉了android.utils.Log的导入行,并添加了以下行:
public static W_Log Log = new W_Log();
因此,我不需要更改现有代码中的"Log. d(" blah "," blah ")"和"Log. e(" blah "," blah ")"。
将codenameone的jar文件添加到库后,我在Android Studio中运行它时,显示了以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp.da, PID: 29854
java.lang.RuntimeException: Unable to start service com.myapp.myapp.DA_Service@859ad2f with Intent { cmp=myapp.da/com.myapp.myapp.DA_Service (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream com.codename1.impl.CodenameOneImplementation.getResourceAsStream(java.lang.Class, java.lang.String)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5248)
at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2444)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream com.codename1.impl.CodenameOneImplementation.getResourceAsStream(java.lang.Class, java.lang.String)' on a null object reference
at com.codename1.ui.Display.getResourceAsStream(Display.java:3104)
at com.codename1.io.Log.print(Log.java:327)
at com.codename1.io.Log.p(Log.java:276)
at com.myapp.myapp.W_Log.i(W_Log.java:29)
at com.myapp.myapp.DA_Service.onStartCommand(DA_Service.java:207)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5230)
at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2444)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
我按照调试步骤深入到codename1类,发现创建的矩形的宽度和高度都为零。
谢谢你的帮助。
1条答案
按热度按时间jgwigjjp1#
这与日志无关。您不能使用Codename One作为库。您需要使用我们的项目模板和生命周期类。您可以在https://start.codenameone.com/生成项目
失败的是Codename One没有初始化,而这在配置了正确的生命周期后会无缝地发生。