others-how to solve the `NullPointerException: Attempt to invoke virtual method` error when trying to check default sms handler in android?
1. Purpose
In this post, I would demonstrate how to solve the following exception when checking the default sms handler in android phones.
2022-03-29 09:28:54.003 4351-4351/com.bswen.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bswen.app, PID: 4351
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:6268)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:6263)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.bswen.myapp.utils.f2.a(SourceFile:3)
at com.bswen.myapp.ui.misc.AboutActivity.onSetAsDefaultSmsHandler(SourceFile:1)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:6263)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2. Code and solution
2.1 The code that cause the error
In the code below, I just want to check the system’s default sms handler:
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void checkDefaultSMS(Activity context) {
if(true) {
//check if this app is the default sms handler.
if (SystemUtils.getApiLevel(context) >= Build.VERSION_CODES.KITKAT) {
if (!Telephony.Sms.getDefaultSmsPackage(context).equals(context.getPackageName())) {
//this app is not default
confirmChangeDefaultSMS(context);
}
}
}
}
The line that cause the above exception is:
if (!Telephony.Sms.getDefaultSmsPackage(context).equals(context.getPackageName())) { //getDefaultSmsPackage return null
2.2 The solution
As you create your app, it’s important to consider the set of other installed apps on the device that your app intends to access. If your app targets Android 11 (API level 30) or higher, the system makes some apps visible to your app automatically, but it filters out other apps by default. This guide describes how to make these other apps visible to your app.
If your app targets Android 11 or higher and needs to interact with apps other than the ones that are visible automatically, add the
<manifest package="com.example.game">
<queries>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/jpeg" />
</intent>
</queries>
...
</manifest>
So the solution is to add <queries>
to your manifest.mf file:
<manifest package="com.example.stuff">
<application>
...
</application>
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="smsto"/>
</intent>
</queries>
</manifest>
Refer to this document for more info.
3. Summary
In this post, I demonstrated how to solve the NullPointerException: Attempt to invoke virtual method
error when trying to check the default sms handler in android. That’s it, thanks for your reading.