Flutter integration with native android ActivityResultLauncher return null object reference on context getPackageName

139 Views Asked by At
  ActivityResultLauncher<String> mGetContent = 
       registerForActivityResult(new ActivityResultContracts.GetContent(),
              uri -> {
                // Handle the returned Uri
              });
    enter code here

  private void MethodName() {

    try {
            mGetContent.launch("image/*");

    }catch (Exception e){
      Log.e("Exception" , e.getLocalizedMessage());
    }
  }

when i call MethodName() i get Exception with this message

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

This error appear with me when use "launch" always

I am using context Like that and already i get package name

  private Context context;
  
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL);
    channel.setMethodCallHandler(this);
    context = flutterPluginBinding.getApplicationContext();
  }
0

There are 0 best solutions below