Hi i've been trying to implement admob ads in this game for a while, i'm always stopped by mAdView = findViewById(R.id.adView);
error, the whole game is in java and i have no layout.xml
file
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mGLSurfaceView = new CCGLSurfaceView(this);
// Create the adView
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// Add the adView to it
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
layout.addView(mGLSurfaceView);
layout.addView(adView);
setContentView(layout);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Try Something like this -
AdView mAdView = new AdView(activity);
RelativeLayout.LayoutParams params = new RelativeLayout
.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mAdView.setLayoutParams(params);
mAdView.setId(R.id.adView);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(activity.getString(R.string.banner_ad_unit_id));
adLayout.removeView(activity.findViewById(R.id.adView));
adLayout.addView(mAdView);;
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
For more check this - https://www.concretepage.com/questions/429
i just found the answer and you have no idea how stupid i was for not trying it once; the solution was to simply delete this from code" mAdView = findViewById(R.id.adView); "
If you are creating everything in code; then what's the need to add this line?
mAdView = findViewById(R.id.adView);
Remove this and you are good to go.
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.