NullObjectReference for Admob in different activities - android

I had read a few q&a in stackoverflow, but still not able to get it work.
Admob on Multiple Activities?
Similar to above solutions, I got the below error
android.content.res.Resources android.content.Context.getResources()' on a null object reference
in this line
AdView adView = new AdView(this);
Can anyone find out why it happens?
CommonCode.java
public class CommonCode extends AppCompatActivity{
//private AdView mAdView;
public void createAdview(){
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CommonCode cc = new CommonCode();
cc.createAdview();}}

You extends AppCompatActivity in CommonCode that define CommonCode as an Activity.Activity context is null untill it start.
You need to remove extnds AppCompatActivity from CommonCode class and pass the context in a parameter as below :
public class CommonCode{
public void createAdview(Context context){
AdView adView = new AdView(context);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Now in a MainActivity you need to call the method like this :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CommonCode cc = new CommonCode();
cc.createAdview(this);
}
}

You need to pass activity context in this line
AdView adView = new AdView(this);
Create common class like this
public class CommonCode {
public CommonCode (){
}
public void createAdview(Context context){
AdView adView = new AdView(context);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}}
And use it like this in your activity
CommonCode cc = new CommonCode();
cc.createAdview(this);

Related

Unable to show Admob ads in android activity

I am trying to implement Admob ads in android activity but after everything done as per instruction given on firebase-Admob ads integration guide there is problem in showing ads in activity.What I have done so far is:
AndroidManifest.XML
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="MY_APP_ID" />
XML
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/ad_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_ADUNIT_ID">
</com.google.android.gms.ads.AdView>
Java
public class Ad extends AppCompatActivity {
AdView ad_View;
AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
MobileAds.initialize(this,#string/appid);
ad_View = findViewById(R.id.adView);
ad_View = new AdView(this);
ad_View.setAdSize(AdSize.SMART_BANNER);
ad_View.setAdUnitId(#string/adunit);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
ad_View.loadAd(adRequest);
}
}
Someone please let me know what I am doing. Any help would be appreciated.
THANKS
Please try to remove this lines from your code,
ad_View = new AdView(this);
ad_View.setAdSize(AdSize.SMART_BANNER);
ad_View.setAdUnitId(#string/adunit);
because you have already set in XML
So try this,
public class Ad extends AppCompatActivity {
AdView ad_View;
AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
MobileAds.initialize(this,#string/appid);
ad_View = findViewById(R.id.adView);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
ad_View.loadAd(adRequest);
}
}
Note : Please make sure to add Internet Permission in AndroidManifest.xml

My question about add google ads in android app

java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'
on a null object r
public class Activity1 extends Activity {
Button btnrateapp, btnnext,btnshareapp;
int share,rate;
private AdView adView;
private InterstitialAd mInterstitialAd;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
// MobileAds.initialize(this, "ca-app-pub-3297215203354227~5525786209");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-9817937406815779/5109921994");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
adView =findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
//mAdView.loadAd(new AdRequest.Builder().build());
adView.loadAd(adRequest);
You must add this code in your layout activity1.xml:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ad_unit_id_for_banner_here"/>

My application does not appear admob ads

In my application AdMob ads do not appear please help and here is my code.
thank you.
code:
public class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view) {
super(view);
MobileAds.initialize(getContext(),"ca-app-pub-3790716330382724~3051722333");
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
}
}

Android InterstitalAd gray small bar bevor the ad

Im trying to get a InterstitialAd into my application.
It works now,
However:
1) I always get a small gray bar displayed on top of the screen during the ad is loading
2) The ad takes really long to load
any ideas how to fix these two problems?
BannerActivity:
public class BannerActivity extends Activity{
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-XXXXXX");
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded(){
mInterstitialAd.show();
}
#Override
public void onAdClosed() {
finish();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXXXXXXXXXX").build();
mInterstitialAd.loadAd(adRequest);
}
}
How i load the banner:
Intent bannerIntent = new Intent(myContext, BannerActivity.class);
myContext.startActivity(bannerIntent);
xml:
android:name="kletzgames.crazyballs.BannerActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I fixed it:
I now call the Ad during another View so it can load, and ones it loaded it will be displayed during screenTransaction.
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXX").build();
mInterstitialAd.loadAd(adRequest);
** some if's **
mInterstitialAd.show();

LibGDX ad control tutorial producing null pointer

I have been following this tutorial: https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx
I'm sure i have implemented everything correctly and am still getting a null pointer for the handler. Is there something wrong with the code in the tutorial?
Here is my Android Launcher Code:
public class AndroidLauncher extends AndroidApplication implements IActivityRequestHandler{
protected AdView adView;
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SHOW_ADS:
{
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS:
{
adView.setVisibility(View.GONE);
break;
}
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new PBGame(this));
// Create and setup the AdMob view
AdView adView = new AdView(this);
adView.setAdUnitId("Secret Key");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.addTestDevice("Test Device")
.build());
// Add the libgdx view
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}
#Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}
}
My Game Class:
public static final int VIRTUAL_WIDTH = 800;
public static final int VIRTUAL_HEIGHT = 480;
public static final float ASPECT_RATIO =
(float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
public static final int zeroMakerX = 400, zeroMakerY = 240;
public static Camera camera;
public static Rectangle viewport;
private IActivityRequestHandler myRequestHandler;
public PBGame(IActivityRequestHandler handler) {
myRequestHandler = handler;
}
#Override
public void create() {
AssetHandler.load();
super.setScreen(new TitleScreen(this));
AssetHandler.music.play();
AssetHandler.music.setLooping(true);
}
#Override
public void dispose() {
super.dispose();
}
}
Finally the ReqestHandler:
public interface IActivityRequestHandler {
public void showAds(boolean show);
}
The problem is as follows:
Your AdView object is defined locally inside the onCreate() function of the AndroidLauncher class. You then attempt to access it outside of onCreate() in the Handler object. The AdView object is out of scope. You should declare the AdView in your AndroidLauncher class outside of onCreate():
AdView adView;
Then in onCreate() you can instantiate it as you did:
// Create and setup the AdMob view
adView = new AdView(this);
adView.setAdUnitId("Secret Key");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.addTestDevice("Test Device")
.build());

Categories

Resources