Android Question [Solved] read the events from a specific google calendar - error

AnandGupta

Expert
Licensed User
Longtime User
I need to read the events from a specific google calendar (I have a few on my phone) and list them date wise.

Searching our Forum I found Don's very good library and explanation,

I assume this is the way to go in current version.

I got error in B4XPages, may be I made some mistake somewhere, so I tried Activity logic, as the explanation is based on it.
I copy pasted the codes as explained but still got error.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    canAccessCal = False
    Activity.LoadLayout("Layout")

    Starter.rp.CheckAndRequest("android.permission.READ_CALENDAR")
    wait for Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        Log("NO Permission READ Calendar")
    Else
        Starter.rp.CheckAndRequest("android.permission.WRITE_CALENDAR")
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            canAccessCal = True
            cr.Initialize("CR")
        End If
    End If
    If canAccessCal Then
        ' We are working from here now...
        Log("can read write calendar")
        
        Dim projection() As String = Array As String(ccon.ID,ccon.NAME,ccon.CALENDAR_DISPLAY_NAME,ccon.ACCOUNT_NAME,ccon.ACCOUNT_TYPE,ccon.OWNER_ACCOUNT)
        
        cr.QueryAsync(ccon.CONTENT_URI,projection, ccon.VISIBLE&"=1",Null,ccon.ID&" ASC")
        wait for CR_QueryCompleted(Success As Boolean, Crsr As Cursor)
        Log($"QueryCompleted(${Success})"$)
        If Crsr.IsInitialized Then
            If Crsr.RowCount > 0 Then
                Dim Cursor As Cursor
                Cursor = Crsr
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    'calcon.ACCOUNT_TYPE,calcon.OWNER_ACCOUNT)
                    Log($"CR --------------------------------------"$)
                    Log(Cursor.GetString(ccon.ID)) ' You can use the constants you used for the projection. But remember: only the ones you defined in the projection are available in the Result.
                    Log(Cursor.GetString(ccon.NAME))
                    Log(Cursor.GetString(ccon.CALENDAR_DISPLAY_NAME))
                    Log(Cursor.GetString(ccon.ACCOUNT_NAME))
                    Log(Cursor.GetString(ccon.ACCOUNT_TYPE))
                    Log(Cursor.GetString(ccon.OWNER_ACCOUNT))
                Next
                Cursor.Close
            Else
                ' No rows
            End If
        Else
            '
        End If
    End If
End Sub


Error log
B4X:
Logger connected to:  Xiaomi Redmi 4
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
can read write calendar
QueryCompleted(true)
CR --------------------------------------
1
main$ResumableSub_Activity_Createresume (java line: 556)
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
    at anywheresoftware.b4a.BA.addLogPrefix(BA.java:604)
    at anywheresoftware.b4a.keywords.Common.LogImpl(Common.java:191)
    at b4a.GCalenadar.main$ResumableSub_Activity_Create.resume(main.java:556)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6238)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

I think I need to change some string value but do not know which one.
Please help.

Regards,

Anand
 

AnandGupta

Expert
Licensed User
Longtime User
The error happens because you are trying to log a Null string.
Ok.

Checked in Android 5 phone, gives proper log as expected and no error.

Checked in Android 7 and 9 phone, gives same error.
So there must be something more to set like permission or manifest etc. for higher Android versions. Since the original code of Don is old, so I could not get from there any hint or may be I am missing something.

Any guide on it or search result will help. Attached the project zip. It is just showing calendar names, if not error.

Regards,

Anand
 

Attachments

  • GCalendar.zip
    9.7 KB · Views: 281
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Sorry for late reply, was busy with bread and butter.

Yes this works perfectly and shows all my calendar names. Thank you.

Regards,

Anand
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…