Android Question Permission Check - Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()

DaOel

Member
Licensed User
Longtime User
This Code:

B4X:
Dim rp As RuntimePermissions
'Send SMS Not allowed in google Play Store anymore.
rp.CheckAndRequest(rp.PERMISSION_SEND_SMS)

results in the error:


B4X:
** Service (smsandbackupservice) Start **
Service started in foreground mode.
Error occurred on line: 60 (SmsAndBackupService)
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 java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at b4a.fahrstundenPlanerSMS.smsandbackupservice.handleStart(smsandbackupservice.java:100)
    at b4a.fahrstundenPlanerSMS.smsandbackupservice.access$000(smsandbackupservice.java:8)
    at b4a.fahrstundenPlanerSMS.smsandbackupservice$1.run(smsandbackupservice.java:71)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:237)
    at b4a.fahrstundenPlanerSMS.smsandbackupservice.onStartCommand(smsandbackupservice.java:69)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5174)
    at android.app.ActivityThread.access$2100(ActivityThread.java:315)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2324)
    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:8751)
    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:1135)


Any ideas what might be the reason?
 

DaOel

Member
Licensed User
Longtime User
my manifest:

B4X:
'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

AddApplicationText(
        <!-- BroadcastReceiver that listens for incoming SMS messages https://stackoverflow.com/questions/30127564/how-do-i-set-my-app-as-the-default-sms-app -->
        <receiver
           android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver
            android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service
            android:name=".HeadlessSmsSendService"
            android:exported="true"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>
)
AddManifestText(   
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>

<uses-permission android:name="android.permission.RESPOND_VIA_MESSAGE" />
<uses-permission android:name="android.permission.WAP_PUSH_DELIVER" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
    
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.SEND_SMS")
AddPermission("android.permission.RECEIVE_SMS")



'SetReceiverAttribute(SmsAndBackupService, android:permission, "android.permission.BROADCAST_SMS")
'AddReceiverText(SmsAndBackupService, <intent-filter>
'  <action android:name="android.provider.Telephony.SMS_DELIVER" />
'  </intent-filter>
')
'SetReceiverAttribute(SmsAndBackupService, android:permission, "android.permission.BROADCAST_WAP_PUSH")
'AddReceiverText(SmsAndBackupService,  <intent-filter>
'  <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
'  <data android:mimeType="application/vnd.wap.mms-message" />
'  </intent-filter>
')
'SetServiceAttribute(SmsAndBackupService, android:permission, "android.permission.SEND_RESPOND_VIA_MESSAGE")
'SetServiceAttribute(SmsAndBackupService, android:exported, "true")
'AddServiceText(SmsAndBackupService,  <intent-filter>
'  <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
'  <category android:name="android.intent.category.DEFAULT" />
'  <data android:scheme="sms" />
'  <data android:scheme="smsto" />
'  <data android:scheme="mms" />
'  <data android:scheme="mmsto" />
'  </intent-filter>
')
'AddActivityText(main,
' <intent-filter>
'  <action android:name="android.intent.action.SEND" />   
'  <action android:name="android.intent.action.SENDTO" />
'  <category android:name="android.intent.category.DEFAULT" />
'  <category android:name="android.intent.category.BROWSABLE" />
'  <data android:scheme="sms" />
'  <data android:scheme="smsto" />
'  <data android:scheme="mms" />
'  <data android:scheme="mmsto" />
'  </intent-filter>
')






SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

SetApplicationAttribute(android:theme, "@style/DarkTheme")
CreateResource(values, Colors.xml,
    <resources>
    <color name="actionbar">#ff039be5</color>
    <color name="statusbar">#ff006db3</color>
    <color name="navigationBar">#ff006db3</color>
    </resources>
)
CreateResource(values-v20, theme.xml,
<resources>
    <style
        name="DarkTheme" parent="@android:style/Theme.Material">
        <item name="android:colorPrimary">@color/actionbar</item>
        <item name="android:colorPrimaryDark">@color/statusbar</item>
        <item name="android:navigationBarColor">@color/navigationBar</item>
    </style>
</resources>
)
CreateResource(values-v14, theme.xml,
<resources>
    <style
        name="DarkTheme" parent="@android:style/Theme.Holo">
        <item name="android:colorPrimary">@color/actionbar</item>
        <item name="android:colorPrimaryDark">@color/statusbar</item>
        <item name="android:navigationBarColor">@color/navigationBar</item>
    </style>
    
</resources>
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)
CreateResourceFromFile(Macro, GooglePlayBilling.GooglePlayBilling)
 
Upvote 0

DaOel

Member
Licensed User
Longtime User
I guess sending sms works now for me.
However my app crashes whenever I receive an sms.

In order to list my app as 'standard app' for sms, I needed to add this to the manifest:

B4X:
<!-- BroadcastReceiver that listens for incoming SMS messages https://stackoverflow.com/questions/30127564/how-do-i-set-my-app-as-the-default-sms-app -->
        <receiver
           android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

Now i can choose my app from the list.

However when I receive an SMS I get this error:

B4X:
java.lang.RuntimeException: Unable to instantiate receiver b4a.fahrstundenPlanerSMS.SmsReceiver: java.lang.ClassNotFoundException: Didn't find class "b4a.fahrstundenPlanerSMS.SmsReceiver" on path: DexPathList[[zip file "/data/app/~~xG5NcpNj-Wr-hViHa_Wsiw==/b4a.fahrstundenPlanerSMS-ti_ugiTXux-KLzNuFprFMQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~xG5NcpNj-Wr-hViHa_Wsiw==/b4a.fahrstundenPlanerSMS-ti_ugiTXux-KLzNuFprFMQ==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:4808)
    at android.app.ActivityThread.access$1700(ActivityThread.java:315)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2297)
    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:8751)
    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:1135)
Caused by: java.lang.ClassNotFoundException: Didn't find class "b4a.fahrstundenPlanerSMS.SmsReceiver" on path: DexPathList[[zip file "/data/app/~~xG5NcpNj-Wr-hViHa_Wsiw==/b4a.fahrstundenPlanerSMS-ti_ugiTXux-KLzNuFprFMQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~xG5NcpNj-Wr-hViHa_Wsiw==/b4a.fahrstundenPlanerSMS-ti_ugiTXux-KLzNuFprFMQ==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.AppComponentFactory.instantiateReceiver(AppComponentFactory.java:110)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:4801)
    ... 9 more

(Actually I am not interested in receiving sms logic, but I need the app to be in the standard app list.)
I added a SmsReceiver Service like described here:


(Also added the entries to the manifest as described in the link. Still I needed this <receiver> entry in the manifest to keep it as standard app for sms.)
The same error occurs every time I receive a sms message.
 
Upvote 0

DaOel

Member
Licensed User
Longtime User
the error says it doesnt find a class 'SmsReceiver'. I just created that class but that was not sufficient. Also a service is not sufficient (like described in the documentation)
The service looks like this:

1673538389900.png
 
Upvote 0
Top