简体   繁体   中英

Flutter:PlatformException(error, Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)'

I am trying to enable my app as admin using this plugin.

This is my code below:

  @override
  void initState() {
    super.initState();  
    Admin.enable();            
    _callStreamSubscription = phoneStateCallEvent.listen((PhoneStateCallEvent event) {
      print('Call is Incoming or Connected: ' + event.stateC);  
      if(event.stateC=="true"){        
        _invokeNativeMethod("startRecording");
      }
      else if(event.stateC=="false"){        
        _invokeNativeMethod("stopRecording");      
      }
    });   
  }

Error is as follows:

E/flutter ( 8036): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference, null)
E/flutter ( 8036): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 8036): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter ( 8036): <asynchronous suspension>                            
E/flutter ( 8036): #2      Admin.enable (package:admin/admin.dart:10:21)
E/flutter ( 8036): #3      _MyHomePageState.initState (package:automatic_call_recorder/main.dart:55:11)

Any help would really be appreciated!!

So, I got the solution:

Actually, I tried adding the code in my old package where I had platform-specific code in which I had removed this line

GeneratedPluginRegistrant.registerWith(flutterEngine);

Because of which the error was occuring. So I added the line again and it works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM