2

I am trying to show a Banner Ad from native java code to Flutter. I've tried seeing multiple plugins to write my code however when my app debugs it always shows me the error - Unhandled Exception: PlatformException(error, Cannot add a null child view to a ViewGroup, null, java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup

Here's my Java code using PlatformViewFactory -

public class BannerFactory extends PlatformViewFactory {
    private final BinaryMessenger messenger;

    public BannerFactory(BinaryMessenger messenger) {
        super(StandardMessageCodec.INSTANCE);
        this.messenger = messenger;
    }

    @Override
    public PlatformView create(Context context, int viewId, Object args) {
        return new BannerUtil(context, this.messenger, viewId, (HashMap) args);
    }
}

This is my actual BannerUtil class that contains the callbacks for my Banner Ad.

public class BannerUtil implements PlatformView {
    // private FrameLayout layout;
    private static final String TAG = "BannerUtil";
    private int height;
    private int width;
    private MethodChannel channel;
    private int size;
    private String adUnitId;
    private String placementId;
    private boolean closeButton;
    private int refreshTime;
    private MBBannerView mtgBannerView;

    public BannerUtil(Context context, BinaryMessenger messenger, int id, HashMap args) {
        try {
            this.size = (int) args.get("size");
            this.adUnitId = (String) args.get("adUnitId");
            this.placementId = (String) args.get("placementId");
            this.closeButton = (boolean) args.get("closeButton");
            this.refreshTime = (int) args.get("refreshTime");
            this.height = (int) args.get("height");
            this.width = (int) args.get("width");
            this.channel = new MethodChannel(messenger, "flutter_mintegral");
            // this.layout = new FrameLayout(FlutterMintegralPlugin.activity);
        mtgBannerView = new MBBannerView(FlutterMintegralPlugin.activity);
        mtgBannerView.init(new BannerSize(BannerSize.DEV_SET_TYPE, width, height), placementId, adUnitId);
        mtgBannerView.setAllowShowCloseBtn(closeButton);
        mtgBannerView.setRefreshTime(refreshTime);
        mtgBannerView.setBannerAdListener(new BannerAdListener() {
            @Override
            public void onLoadFailed(String msg) {
                Log.e(TAG, "on load failed" + msg);
            }

            @Override
            public void onLoadSuccessed() {
                Log.e(TAG, "on load successed");
            }
        });
        if (mtgBannerView != null) {
          //  this.layout.removeAllViews();
            mtgBannerView.load();
            // FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
            //     FrameLayout.LayoutParams.MATCH_PARENT);
            // this.layout.addView(mtgBannerView,0,layoutParams);
            // layout.setVisibility(View.VISIBLE);
        }
            
        } catch (Exception e) {
            Log.e("MintegralSDK", e.toString());
        }
    }

    @Override
    public View getView() {
        return mtgBannerView;
    }

    @Override
    public void dispose() {
        //this.layout.removeAllViews();
        if (mtgBannerView != null) {
            mtgBannerView.release();
        }
    }
}

I need help as i'm unsure how to call FrameLayout from Java to call the banner ad in Flutter.

I am even registering my banner ad in my plugins main class like this -

private void RegistrarBanner(PlatformViewRegistry registry, BinaryMessenger messenger) {
        registry.registerViewFactory("/Banner", new BannerFactory(messenger));
    }


public static void registerWith(Registrar registrar) {
    registrar.platformViewRegistry().registerViewFactory("/Banner", new 
    BannerFactory(registrar.messenger()));
}

@Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        this.flutterPluginBinding = flutterPluginBinding;
        this.RegistrarBanner(flutterPluginBinding.getPlatformViewRegistry(), flutterPluginBinding.getBinaryMessenger());
    }

This is my Dart part of code (of my internal plugin) which calls AndroidView -

Container(
      width: this.sizes[this.widget.size].width,
      height: this.sizes[this.widget.size].height,
      child: AndroidView(
        viewType: '/Banner',
        key: UniqueKey(),
        creationParams: {
          'closeButton': widget.closeButton,
          'refreshTime': widget.refreshTime,
          'adUnitId': widget.adUnitId,
          'placementId': widget.placementId,
          'size': this.sizes[this.widget.size].type,
          'height': this.sizes[this.widget.size].height,
          'width': this.sizes[this.widget.size].width
        },
        creationParamsCodec: StandardMessageCodec(),
      ),
    );

I'm unsure as to where I'm going wrong? Can someone please help me.

Arnav
  • 1,404
  • 2
  • 19
  • 38

0 Answers0