adView throwing Null pointer exception - android

Please find the code below adView was giving nullpointerexception.
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("ca-app-pub-6117491189465387/7628536955");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView)findViewById(R.id.adView);
// Request for Ads
AdRequest.Builder adRequestBuilder=new AdRequest.Builder();
// Add a test device to show Test Ads
adRequestBuilder .addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adRequestBuilder.addTestDevice("abcd").build();
// Load ads into Banner Ads
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-6117491189465387/7628536955");
adView.loadAd(adRequestBuilder.build());
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
I was struck since 2 day's please let me know where I was mistaken

I don't see your setContentView() method(). adview could be null because you either don't set the layout to use or you call setContentView() after initializing the adview. You should have this:
setContentView(R.layout.activity_main);
AdView adView = (AdView)findViewById(R.id.adView);
Please post the full content of the onCreate() method, your logcat stacktrace and layout file, if I'm wrong

Related

Admob slows my application

I am working with frame animations and i implement banner and interstitial ads in my activity, but when i do that it slows my application.
Here is my code:
private InterstitialAd interstitial;
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("UNIT ID");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
Any solutions on how to efficiently integrate admob in my class.
Answere to your comment
can i put all my admob code in someother class and initialize it in my
Main Activity?
Yes, For example: AdMob.java class:
import android.content.Context;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class AdMob {
private InterstitialAd interstitial;
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
void LoadInterstitialAd(Context ctx){
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(ctx);
// Insert the Ad Unit ID
interstitial.setAdUnitId("unit_id");
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
}
You can use it anywhere by doing this:
AdMob adMob = new AdMob();
adMob.LoadInterstitialAd(getApplicationContext());
I have only showed for interstitial ad, but it can be done for other ads too..

Interstitial and banner ads fail to load

I've been working on this code where apparently the interstitial and banner ads keep failing to load.
Here's the code for creating the banner adview and adding the newly generated ad units. Also the code for interstitial ads which is supported with an adlistener to see if the ads work..
//Ads ----------------
// Create the adView
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//<!-- Ads Using Google Play Services SDK -->
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// 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);
//New AdRequest
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
//-----------------------------------------------------Interstitial Add
// Create an Interstitial ad.
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
interstitialAd.loadAd(new AdRequest.Builder().build());
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
interstitialAd.show();
}
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Interstitial Ads loading failed", Toast.LENGTH_SHORT).show();
}
});
// Load the interstitial ad.
showInterstitialAds();
public void showInterstitialAds()
{
runOnUiThread(new Runnable() {
public void run() {
AdRequest interstitialAdRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(interstitialAdRequest);
}
});
}
In the app, all I get is the Toast message declaring that the interstitial ads don't work. Even the banner ads don't work.
On the logcat, I get different messages:
I last got:
01-17 16:54:05.765: I/Ads(1136): No fill from ad server.
01-17 16:54:05.769: I/Ads(1136): Scheduling ad refresh 60000 milliseconds from now.
01-17 16:54:05.769: W/Ads(1136): Failed to load ad: 3
and there's this:
01-17 16:52:02.945: I/Ads(1136): Starting ad request.
01-17 16:52:02.945: I/Ads(1136): Use AdRequest.Builder.addTestDevice("A55B0AA295150C0583B3FADA0B02054B") to get test ads on this device.
I tried the above in the showInterstitialAds but I kept receiving that same error and ads didn't show.
Also, the most interesting one is:
There was a problem getting an ad response. ErrorCode: 2
With different numbers on the errocode: 0,2,3. I'm not sure what it means as I'm new to the whole android world (I just started coding a few months ago.)
Banner ads aren't loading because there are no ads available. That's what "No fill from ad server means". It happens occasionally especially when your traffic is low.
To test you should be configuring your AdView to show test ads.
See https://developer.android.com/reference/com/google/android/gms/ads/AdRequest.Builder.html#addTestDevice(java.lang.String)
Hard to say what is going wrong with your interstitial.
Are you using the correct AdUnitId?
Also never call interstitial#show() from onAdLoaded(). It' provides a very bad user experience and will probably get your account banned.
Instead you should call interstial#show() from a natural break point in your app, like at the end of a game level.

Not able to see any Ad in by AdView Banner using AdMob

Here is my Activity class
public class MainActivity extends Activity {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("Ad-ID");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView)findViewById(R.id.adView);
// Request for Ads
AdRequest.Builder adRequestBuilder=new AdRequest.Builder();
// Add a test device to show Test Ads
adRequestBuilder .addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adRequestBuilder.addTestDevice("abcd").build();
// Load ads into Banner Ads
//adView.setAdUnitId("Ad-ID");
adView.loadAd(adRequestBuilder.build());
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}//end of onCreate
protected void displayInterstitial() {
// TODO Auto-generated method stub
if (interstitial.isLoaded()) {
interstitial.show();
}
}
This is my simple XML Layout
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="Ad-ID" />
I tried running the application there is no error shown and I am not able to seen any sample ad's in my layout when I run it.
Please let me know where was I mistaken.
You need to replace your own Ad Unit Id in your code and xml file.
adRequestBuilder.addTestDevice("abcd").build(); Here abcd is not a vaild device id. If you are running your app on a real device then in Logcat you will find its device id.
You will have to put that id here in spite of abcd.
In Logcat you will get a line saying,
To get test ads on this device, call adRequest.addTestDevice("SOME_ID_HERE");
You will have to put this id shown here in your code.

Callback is not obtained at onAdLoaded

Iam trying to display ad using Admob,but Iam not getting any call at onAdloaded.Can someone help...
Iam displaying an inerstitial ad.Iam setting and then Iam trying to load it...
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
CalligraphyConfig.initDefault(getString(R.string.font_alegreya_sans_regular));
setInterstitialAd();
setContentView(R.layout.splashscreen_activity);
}
private void setInterstitialAd() {
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId(INTERSTITIAL_AD_ID);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
mInterstitial.loadAd(adRequest);
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
}
});
}
private void initAdView() {
AdView mAdView = (AdView) findViewById(R.id.ad_container_layout);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
You should'nt have displayInterstitial() inside onAdLoaded(). This is causing the outcome of Ad not being shown. Either show an Ad after some level or user made action like after five level, one is displaying an interstitial, it comes in the pause menu. you keep it loaded from before and display it on the fifth level. or you can have it in that pause menu after some amount of time using a timer ( in which displayInterstitial() ) is included.
Suraj, add some log into both callback methods. One of them will be called.
I suspect the ad request is failing for reason, so onFailedToLoad() will be getting called instead.
Also DON'T call displayInterstitial() from onAdLoaded(). This causes really poor user experience and is likely to get your Admob account banned.

Interstitial ad using admob not displayed in released app

My question is that Admob Interstitial ad is displaying in testing mode but if app is released then ad is not displayed.
like i use
AdRequest mAdRequest;
mAdRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
`enter code here`.addTestDevice("12312132654654232").build();
but at the time releasing app i used code like this-
AdRequest mAdRequest;
mAdRequest = new AdRequest.Builder().build();
enter code here
this line not show ad.
Did you use these line in your code
first...
private InterstitialAd interstitial;
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(MY_AD_UNIT_ID);
then use this lines of code
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
and whenever you want to show add call this method
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
hope this will help you.

Categories

Resources