应用无法在我的真实设备上加载非页内广告

时间:2016-07-31 18:54:07

标签: java android admob ads interstitial

测试插页式广告正在我的模拟器上运行。但它们不适用于我的真实设备。

收到广告回复时出现问题。 ErrorCode: 0

W/Ads: Failed to load ad: 0

我在mInterstitialAd.show();

上呼叫onNavigationItemSelected

这是我的主要活动代码

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) {

}

任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

今天我发现了什么问题。

我没有在我的onCreate方法上设置我的App ID代码。 这是:MobileAds.initialize(getApplicationContext(), "your AppID");

您可以在AdMob帐户中找到自己的AppID。

现在测试和真实广告正在发挥作用!谷歌的补充指南错过了它。他们只在横幅教程中写下来......这就是我发现的方式。