2017-08-09 14 views
0

には表示されません。AdMobのバナーは、私は公式ドキュメントに従ってください、私のmainActivity

https://developers.google.com/admob/android/banner

しかし、私は私のコードに誤りを見つけることができません。私は変更しようとしました

AdRequest adRequest = new AdRequest.Builder().build(); 

テストAdRequestについてはまだ動作していません。あなたがビルダーにテストデバイスIDを追加する必要があります

https://github.com/sssolammm/HelloWordTestBanner

public class MainActivity extends AppCompatActivity { 

private AdView mAdView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    MobileAds.initialize(this, "ca-app-pub-5676983055182772~7282283135"); 

    mAdView = (AdView) findViewById(R.id.ad_View); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    mAdView.loadAd(adRequest); 

    mAdView.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      // Code to be executed when an ad finishes loading. 
      Log.i("Ads", "onAdLoaded"); 
     } 

     @Override 
     public void onAdFailedToLoad(int errorCode) { 
      // Code to be executed when an ad request fails. 
      Log.i("Ads", "onAdFailedToLoad"); 
     } 

     @Override 
     public void onAdOpened() { 
      // Code to be executed when an ad opens an overlay that 
      // covers the screen. 
      Log.i("Ads", "onAdOpened"); 
     } 

     @Override 
     public void onAdLeftApplication() { 
      // Code to be executed when the user has left the app. 
      Log.i("Ads", "onAdLeftApplication"); 
     } 

     @Override 
     public void onAdClosed() { 
      // Code to be executed when when the user is about to return 
      // to the app after tapping on an ad. 
      Log.i("Ads", "onAdClosed"); 
     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.my, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** Called when leaving the activity */ 
@Override 
public void onPause() { 
    if (mAdView != null) { 
     mAdView.pause(); 
    } 
    super.onPause(); 
} 

/** Called when returning to the activity */ 
@Override 
public void onResume() { 
    super.onResume(); 
    if (mAdView != null) { 
     mAdView.resume(); 
    } 
} 

/** Called before the activity is destroyed */ 
@Override 
public void onDestroy() { 
    if (mAdView != null) { 
     mAdView.destroy(); 
    } 
    super.onDestroy(); 
} 
} 

のXmlレイアウト

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      xmlns:ads="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="@dimen/activity_vertical_margin" 
      tools:context=".MainActivity" 
      tools:ignore="MergeRootFrame"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/activity_horizontal_margin" 
    android:layout_marginStart="@dimen/activity_horizontal_margin" 
    android:text="@string/hello_world" /> 

<com.google.android.gms.ads.AdView 
    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="BANNER" 
    ads:adUnitId="@string/banner_ad_unit_id"> 
</com.google.android.gms.ads.AdView> 

</RelativeLayout> 
+0

と "onAdFailedToLoadは" ログにapearsかどうかを確認します。 – 1615903

答えて

0

これは私のソースコードです。 テストIDで動作する場合は、本番環境で動作します。

AdRequest request = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators .addTestDevice("your id") // Put here your test device id .build();

は、テストデバイスIDについて、詳細な検索フィールドAdRequestに入れに入れて、実行アプリの後Logcatに行くので、IDデバイスがダウンを示しています。

0

まあ、広告が表示されない場合があります。私は自分のアプリケーションをアンインストールし、ビルドのバリエーションをデバッグからリリースに変更します。

その後、もう一度アプリをインストールしてください...バナー広告が正常に表示されます。

バナー広告が表示されない場合でも、ログ情報を確認してテストデバイスidを取得します。このテストデバイスIDをAdRequestオブジェクトに追加してください...バナー広告が表示されます。 loadAdを呼び出す前にadListenerを設定

adRequest.addTestDevice( "あなたのID")