Android Question Problem with Printing Library

PierPaduan

Active Member
Licensed User
Longtime User
Hi,
I'm migrating from PDFWriter Library to Printing Library to be compatible with all alphabets.
I started from this: https://www.b4x.com/android/forum/threads/printing-and-pdf-creation.76712/#content

My app produce PDFs with text and pictures that usually are about 1MB/page.
The pictures are loaded with LoadBitmapSample and I cannot reduce more the resolution.

When I use a Samsung S4 smartphone with Android 5.0.
In debug mode.

If I create one page PDF is all OK, but if I try to create a multipage file with more than 3 or 4 pages, after that "pdf.WriteToStream(out)" instruction start, my app stops and appear the message "Unfortunately my app has stopped". In the Log of B4A doesn't appear errors. If I put a Try...Catch statement around the pdf.WriteToStream I don't catch anything.
In release mode.
The app stops in the same way. I obtain the same message, but I obtain this error log as well:

B4X:
** Service (starter) Create **
filters_activity_create (java line: 411)
java.lang.RuntimeException: Object should first be initialized (Map).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4a.objects.collections.Map.Get(Map.java:65)
    at it.ritmo.setandgo.filters._activity_create(filters.java:411)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at it.ritmo.setandgo.filters.afterFirstLayout(filters.java:102)
    at it.ritmo.setandgo.filters.access$000(filters.java:17)
    at it.ritmo.setandgo.filters$WaitForLayout.run(filters.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
java.lang.RuntimeException: Object should first be initialized (Map).

When I use a Huawei with Android 4.4.2 in debug or release mode.

The multipage (my maximum target is 10 pages) PDF is created, but when I open it, some pictures are correct, other are corrupted (partially covered by horizzontal lines).

Is this a memory problem? How can I fix it?
Thanks a Lot to everybody can help.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The error logs you posted are not related to printing.

There must be an error message in the logs when the app crashes.
If you are using B4A v7+ then enable logging in release mode with #BridgeLogger: True
Sometimes you can see the crash logs by running the app again.

If you are unable to see the logs with B4A-Bridge then you should switch to USB debug mode.
 
Upvote 0

hears

Active Member
Licensed User
Longtime User
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
Hi,
I will upgrade to B4A v7+ soon.
For the moment my code is this:
B4X:
     For i = 0 To selectedlist.Size -1
                Log("i = " & i )
                Dim arr_temp() As Object
                arr_temp = selectedlist.get(i)
             
                Dim SerialNum As Int = arr_temp(30)
                Dim NumWeld As Int = arr_temp(31)
                Dim da As String = arr_temp(32)
                Dim mo As String = arr_temp(33)
                Dim ye As String = arr_temp(34)
                Dim ho As String = arr_temp(35)
                Dim mi As String = arr_temp(36)
             
             
                pdf.Initialize

                GenerateReportPDF_3( SerialNum, NumWeld, da, mo, ye, ho, mi )

             
                Dim out As OutputStream = File.OpenOutput(File.DirRootExternal & "/SETandGO/Temp", NameFilePdf, False)
                If File.Exists(File.DirRootExternal & "/SETandGO/Temp", NameFilePdf) Then
                    Log("NameFilePdf = " & NameFilePdf)
                    pdf.WriteToStream(out)
                    out.Close
                    pdf.Close
                Else
                    Log("DOESN'T EXIST: " & "/SETandGO/Temp" & NameFilePdf)
                End If
          
Next

I'm running in Debug mode.
The first time all worked fine and the For Next cycle reach the end.
Second time it stopped at last iteration. The error log is the following.
I tried more times to run the cycle. A couple of times it run ok till the end, others not and stops, but the stopping iteration may change. In the other stops I didn't obtained a log of the error.

B4X:
java.lang.RuntimeException: Unable to create service it.ritmo.setandgo.starter: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3167)
    at android.app.ActivityThread.access$1900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:167)
    at anywheresoftware.b4a.shell.Shell.start(Shell.java:99)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:88)
    at it.ritmo.setandgo.starter.onCreate(starter.java:32)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3157)
    ... 9 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at anywheresoftware.b4a.shell.ShellConnector.sendControlMessage(ShellConnector.java:61)
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:127)
    ... 13 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Attempt to invoke virtual method 'void java.io_OutputStream.write(byte[])' on a null object reference

You are closing the PDF in your loop. In 2nd iteration it crashes i guess.
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
You are closing the PDF in your loop. In 2nd iteration it crashes i guess.

On S4, first time it did 10 iterations correctly.
Second time it did 9 iterations. The above error appeared in the 10th iteration.
The stop iteration may change.

On HUAWEI it did 10 iterations correctly every time.

Why?????
Thanks.
 
Upvote 0
Top