Android Question [Solved] RuntimePermissions.CheckAndRequest NullPointerException: Attempt to invoke virtual method WeakReference.get

weighment

Member
Licensed User
This is a Default app with the code below in starter service called from a button click in Main, error is on line 7

B4A 12.20

side note: BLEExample project works on same device doing rp.check in B4XMainPage, but I saw that you generally need to use rp.check in a service. (new project has same error doing rp.check in Main or in a class or in starter service)

This is in starter:
Sub checkPermissions

    Private rp As RuntimePermissions
    Dim phone As Phone
    Log(phone.SdkVersion) ' shows 29

    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    'Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission: " & Permission, True)
        Return
    End If
    Return
    
End Sub

Manifest:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.BLUETOOTH_CONNECT)

Screenshot 2023-03-08 100146.png


Error shown upon click:
Logger connected to:  Datalogic Skorpio X5
--------- beginning of crash
--------- beginning of main
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
29
Error occurred on line: 119 (Starter)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
    at anywheresoftware.b4a.objects.RuntimePermissions.CheckAndRequest(RuntimePermissions.java:94)
    at b4a.example.starter$ResumableSub_checkPermissions.resume(starter.java:176)
    at b4a.example.starter._checkpermissions(starter.java:141)
    at b4a.example.main._button1_click(main.java:424)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7259)
    at android.view.View.performClickInternal(View.java:7236)
    at android.view.View.access$3600(View.java:801)
    at android.view.View$PerformClick.run(View.java:27896)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7397)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
 

weighment

Member
Licensed User
[Solved]
Just saw where rp.CheckAndRequest can only be called from an Activity. (though rp.Check can be called from a service)
Should have been obvious (...AndRequest)
Sorry for the distraction, feel free to delete
 
Upvote 0
Top