jgithaiga / flutter_sms_inbox

Flutter SMS Inbox (Android only)
MIT License
15 stars 17 forks source link

PlatformException(error, Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference #4

Closed nikiforospertselis closed 2 years ago

nikiforospertselis commented 3 years ago

When i run the query .getAllSms() i get the error bellow. All the sms permissions is granted. This is my code.

SmsQuery query = new SmsQuery();
List<SmsMessage> messages = await query.getAllSms.catchError((e) {
      print("Sms error: $e");
    });

I get this error:

E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at android.os.MessageQueue.next(MessageQueue.java:336) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at android.os.Looper.loop(Looper.java:174) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at android.app.ActivityThread.main(ActivityThread.java:7710) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) E/MethodChannel#plugins.juliusgithaiga.com/querySMS(26867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) I/flutter (26867): Sms error: PlatformException(error, Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference I/flutter (26867): at com.juliusgithaiga.flutter_sms_inbox.permissions.Permissions.hasPermission(Permissions.java:25) I/flutter (26867): at com.juliusgithaiga.flutter_sms_inbox.permissions.Permissions.hasPermissions(Permissions.java:30) I/flutter (26867): at com.juliusgithaiga.flutter_sms_inbox.permissions.Permissions.checkAndRequestPermission(Permissions.java:45) I/flutter (26867): at com.juliusgithaiga.flutter_sms_inbox.SmsQueryHandler.handle(SmsQuery.java:61) I/flutter (26867): at com.juliusgithaiga.flutter_sms_inbox.SmsQuery.onMethodCall(SmsQuery.java:189) I/flutter (26867): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) I/flutter (26867): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) I/flutter (26867): at io.flutter.embeddi

AndroidManifest

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
Nasir87 commented 2 years ago

hi buddy, i think you should import permission_handler

import 'package:permission_handler/permission_handler.dart'; ...

// in the space where the error was thrown, you should ask the permission first to read sms, // checkSelfPermission might mean that u requested for data like the permission was already given var sms = await Permission.sms.status; if (sms.isGranted) { SmsQuery query = new SmsQuery(); List messages = await query.getAllSms.catchError((e) { print("Sms error: $e"); }); }

jgithaiga commented 2 years ago

Thanks @Nasir87