Home > Back-end >  Issue with wrapping around com.codename1.io.Log to replace android.utils.log
Issue with wrapping around com.codename1.io.Log to replace android.utils.log

Time:02-03

I am trying to convert an Android app into a codenameone app then an iOS app. The existing Android app uses android.utils.Log that takes two String variables, while the com.codename1.io.Log takes one String variable and an int for logging level. Instead of making changes in existing code, I tried to write a wrapper class W_Log around com.codename1.io.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);
    }
}

In my existing code, I commented out the import line for android.utils.Log and added the following line:

public static W_Log Log = new W_Log();

As a result, I don't need to change the "Log.d("blah", "blah")" and "Log.e("blah", "blah")" in existing code.

When I ran it in Android Studio after adding codenameone's jar files to libs, the following error was shown:

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) 

I followed the debugging steps deep into codename1 classes and saw that the width and height of a rectangle created were both zero.

Thanks for your help.

CodePudding user response:

This has nothing to do with the logs. You can't use Codename One as a library. You need to use our project template and lifecycle classes. You can generate a project at https://start.codenameone.com/

What's failing is that Codename One isn't initialized which happens seamlessly when the proper lifecycle is configured.

  • Related