Android Question Adding Page Turn view to the second Activity

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hello

I asked a few questions on Page Turn View thread but got no answer so I decided to start a thread here.

The view is working great when adding it to main activity but if added to another activity it closes the application with "Unfortunately , app closed ..." message and shows
B4X:
maininitializeProcessGlobals (java line: 371)
java.lang.RuntimeException: java.lang.NullPointerException
on B4A IDE Logs tab

If I change the starting activity with this method
B4X:
SetActivityAttribute(Main, android:name, ".activity2")
SetActivityAttribute(Activity2, android:name, ".main")

The application works as expected.
Now I do not whats wrong , Did I miss something or is it a bug?
Is there any thing I can do?
Thanks in advance
 

Beja

Expert
Licensed User
Longtime User
not following..
if it is solved then what's the problem..
are you duplicating the initialization?
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
If I add page turn view to the second activity module, the application doesn't start at all.
I want to add a starting activity and the page turn view should be on the second activity.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Just as troubleshooting, make sure to delete everything on the top "Region" (Name, Orientation....etc)
open the region
then click on the '+' and you will see this information in red color.. delete all of it because they are already on the first activity.
It is on the top left corner.

Good luck!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do ?
Especially with this ?
SetActivityAttribute(Activity2, android:name, ".main")

I tested the demo program adding a second activity and added a PageTurnView on this second activity.
The second activity is called with a button click from the firat one. And it works.
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
I modified the demo and moved the main activity code to new activity so I have a starting activity instead of just starting directly to the pages.
SetActivityAttribute was just to debug and the application worked after setting pages activity as the starting activity
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Thanks
I copied my page turn view activity's code to your second activity and the same problem occurred again so it might be from my code but the strange thing is that if I copied the code to the first activity - or changed the starting activity to the one that has PageTurn view code - the application seems to work again
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Upvote 0

Beja

Expert
Licensed User
Longtime User
Did you remove the attributes from the second activity?

#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
I just moved the subs so the second activity has only these two
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
That's weird..
I still believe there is duplicate in setting some attributes.. would you remove any confidential code and post the rest of the project?
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Here is the first activity code

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: False
    #ApplicationLabel: App Name
    #VersionCode: 1
    #VersionName: 0.0.1
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region



Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim mainlist As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    go
End Sub
Sub go
    mainlist.Initialize("mainlist")
    Activity.AddView(mainlist,0,0,100%x,100%y)
    mainlist.AddSingleLine("Some Item")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub mainlist_ItemClick (Position As Int, Value As Object)
   
End Sub
Sub mainlist_ItemLongClick (Position As Int, Value As Object)
   


End Sub

And here is the second activity code

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: false
#End Region


Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim PageNumber As Int ' save page number during device rotation
   
'    Dim paracount As Int : paracount =0
    Dim LeftMargin As Int : LeftMargin = 5%x
    Dim TopMargin As Int : TopMargin = 0%y
    Dim LineSpacing As Int : LineSpacing = 1
    Dim ft As Boolean=False
    Dim alltext,all2 As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim PageTurner As PageTurnView
    Dim pager As TextPaginator
    Dim Font As Typeface
End Sub

Sub Activity_Create(FirstTime As Boolean)

    If FirstTime Then
        ft=True
    End If
    PageTurner.Initialize("PageTurner", 20)
    Activity.AddView(PageTurner,0, 0, Activity.Width,Activity.Height )
   
   
    PageTurner.TwoPages = False
'    PageTurner.RenderLeftPage = False
    pager.SetPageParameters(pager.ALIGN_CENTER,  PageTurner.Width , LeftMargin, PageTurner.Height , TopMargin, LineSpacing)
    PageTurner.SetMarginPixels(1dip, 1dip, 1dip, 1dip)

    PageTurner.AllowLastPageCurl = False ' the default is true   
   
    Font=Typeface.LoadFromAssets("bold.ttf")

    pager.SetPaintParameters( Font, 22, Colors.Black, True)

    alltext.Initialize
    all2.Initialize
   
    alltext.AddAll(Array As String("ONe page text", _
    "Another Page", _
    "New page text", _
    "And last page"))
    For l= alltext.Size-1 To 0 Step -1
        Dim ntxt As String =alltext.Get(l)
        pager.Paginate((l)&CRLF&CRLF&CRLF&CRLF&CRLF&CRLF&ntxt& CRLF)
   
    Next


   
End Sub


Sub Activity_Resume
    PageTurner.CurrentPage = PageNumber
    PageTurner.Color = Colors.RGB(245, 241, 222) ' otherwise it gets lost on Pause and Resume without a Create
    PageTurner.OnResume

    If ft = True Then
        PageTurner.CurrentPage=pager.PageCount+1
       
        ft=False
    End If

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    PageNumber = PageTurner.CurrentPage
    PageTurner.OnPause
    If UserClosed Then
        ft=True
    End If
End Sub

' This is run on the main thread to display any exception in PageTurner_GetBitmap
Sub ShowPTError(title As String, msg As String)
    ToastMessageShow(msg&":::" &title,False)
End Sub

' The PageTurnerView events run on a separate thread to the main thread.
' They therefore must not try to manipulate GUI elements.
' -------------------------------------------------------------------------------------------
' DO NOT ATTEMPT TO PAUSE THE DEBUGGER IN ANY OF SUBS BELOW AS IT MAY HAVE UNEXPECTED RESULTS
' -------------------------------------------------------------------------------------------
' This may be fixed in a future version of Basic4android.

Sub PageTurner_GetBitmap(width As Int, Height As Int, Page As Int) As Bitmap 'Called when the Bitmap for the given page number is required. Return the Bitmap
    ' As this is running on a separate thread exceptions will cause the application to force close
    ' and not report the exception as would happen on the main thread so we use a Try Catch block
    ' to trap any errors
    Dim bmp As Bitmap
    Dim cnv As Canvas
    bmp.InitializeMutable(width, Height) ' do this here so we have a valid return in case of an exception
    Try   
        'File.OpenInput("bad", "filename") ' this would cause an exception to be reported on the main thread

        If Page = 0 Then

            cnv.Initialize2(bmp)
            cnv.DrawColor(Colors.RGB(245, 241, 222))

            cnv.DrawText( "END", width/2,45%y, Typeface.LoadFromAssets("bold.ttf"), 24, Colors.Black, "CENTER")
            Return bmp
        Else If Page = pager.PageCount + 1 Then ' alltext.Size Then    '
            cnv.Initialize2(bmp)
            cnv.DrawColor(Colors.RGB(245, 241, 222))
            cnv.DrawText("", width/2, 1%y, Typeface.LoadFromAssets("arab2.ttf"), 24, Colors.Black, "CENTER")
           
            cnv.DrawText( "Book Name", width/2, 45%y, Typeface.LoadFromAssets("bold.ttf"), 24, Colors.Black, "CENTER")
            cnv.DrawText("", width/2, 98%y, Typeface.LoadFromAssets("arab2.ttf"), 24, Colors.Black, "CENTER")
            Return bmp
        Else
            cnv.Initialize2(bmp)
            cnv.DrawColor(Colors.RGB(245, 241, 222))

            Return pager.GetPageBitmap(Page - 1,Colors.RGB(245, 241, 222))
        End If   
    Catch
        ' catch and report any exceptions on the rendering thread to the main thread
        PTException
    End Try   
    Return bmp ' if we don't return something valid we will cause an exception on the rendering thread

End Sub

Sub PageTurner_GetPages() As Int 'Called when the number of pages is required. Return the number of pages
    ' This is running on a separate thread and I have seen a NullPointerException in here on closing the app
    ' Presumably the Pager object was destroyed before the OpenGL thread was stopped so we trap any error
    Try
        Return pager.PageCount + 1
    Catch
        Return 0 ' if we don't return something valid we will cause an exception on the rendering thread
    End Try
End Sub

Sub PTException()
    Dim Ex As ExceptionEx
    Dim where As String
    Ex = LastException
    Dim args(2) As Object
    args(0) = LastException.Message
    where = Ex.StackTraceElement(2) ' get Java line of error
    args(1) = where   
    PageTurner.RunOnGuiThread("ShowPTError", args)
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks somed3l3oger,
Still can see some activity attributes in the second activity! PLese
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: false
#End Region

because these are dublicate in the first activity.

Can you take off even these including the '#Region' and '#End Region'

just start the activity from Process Globals.
 
Upvote 0
Top