Java – app cannot load non on page ads on my real device
•
Android
The test insert ads are running on my simulator. But they are not suitable for my real device
There was a problem getting the ad response. Errorcode: 0
W/Ads: Failed to load ad: 0
I am calling mminterstitialad. Show(); on onNavigationItemSelected
This is my main activity code
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OddsConversionFragment.OnFragmentInteractionListener,
KellyCalculatorFragment.OnFragmentInteractionListener, PoissonCalculatorFragment.OnFragmentInteractionListener,
TrixieFragment.OnFragmentInteractionListener{
private static final String TAG = "ANALYTICS";
private Tracker mTracker;
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupDrawerToolbar();
createAndAddFragment();
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
//Ads
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-82304873078*******"); //My Ad ID
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
}
});
requestNewInterstitial();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("2CDE4B2B9E906AA1AB8A4EF9690AA6B9")
.build();
//2CDE4B2B9E906AA1AB8A4EF9690AA6B9 Device ID for
mInterstitialAd.loadAd(adRequest);
}
private void createAndAddFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
OddsConversionFragment oddsConversionFragment = new OddsConversionFragment();
fragmentTransaction.add(R.id.fragment_container, oddsConversionFragment);
fragmentTransaction.commit();
}
private void replaceFragment(Fragment fragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
private void setupDrawerToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle(getString(R.string.odds_calculator));
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_close)
.setTitle("Closing Application")
.setMessage("Are you sure you want to close the app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_oddsCalculator) {
replaceFragment(new OddsConversionFragment());
setToolbarTitle(getString(R.string.odds_calculator));
showScreen(getString(R.string.odds_calculator));
} else if (id == R.id.nav_kellyCalculator) {
replaceFragment(new KellyCalculatorFragment());
setToolbarTitle(getString(R.string.kelly_calculator));
showScreen(getString(R.string.kelly_calculator));
} else if (id == R.id.nav_poissonCalculator) {
replaceFragment(new PoissonCalculatorFragment());
setToolbarTitle(getString(R.string.poisson_calculator));
showScreen(getString(R.string.poisson_calculator));
} else if (id == R.id.nav_trixie_calculator) {
replaceFragment(new TrixieFragment());
setToolbarTitle(getString(R.string.trixie_calculator));
showScreen(getString(R.string.trixie_calculator));
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void showScreen(String screenName) {
Log.i(TAG, "Setting screen name: " + screenName);
mTracker.setScreenName(screenName);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
private void setToolbarTitle(String name) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(name);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
Any help would be appreciated
resolvent:
I found something wrong today
I didn't set my app ID code on the oncreate method. It is: mobileads. Initialize (getapplicationcontext(), "your appid");
You can find your appid in your AdMob account
Now testing and real advertising are working! Google's supplementary guide missed it. They only wrote it in the banner tutorial... That's the way I found it
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码