Ik heb dit in mijn XML-hoofdbestand:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="******************"
ads:adSize="BANNER"
android:id="@+id/adView"/>
Ik heb het advertentieformaat en de unit-ID al ingesteld, maar wanneer dit wordt uitgevoerd (vanaf MainActivity.java),
AdView adView = (AdView)this.findViewById(com.example.lovetestactual.R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
Het zorgde voor een uitzondering op wat in de titel staat.
Antwoord 1, autoriteit 100%
Ik heb een oplossing gevonden in het github-voorbeeld, namelijk:
in plaats van
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
verwijder de xmlns:ads***-tag en voeg toe
xmlns:ads="http://schemas.android.com/apk/res-auto"
tag als volgt aan uw LinearLayout-tag:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>
Dat is het 🙂
Antwoord 2, autoriteit 14%
Waarschuwing:zorg ervoor dat u het advertentieformaat en de advertentieblok-ID op dezelfde manier instelt (d.w.z. beide in XML of beide programmatisch).
https://developers.google.com/admob/android/banner
Antwoord 3, autoriteit 3%
Dit wordt vaag omdat je niet hebt aangegeven welk type weergave je XML-bestand is (relatief, lineair, enz.)
In mijn applicatie met een schuifbare relatieve lay-out die ik heb opgenomen:
<RelativeLayout
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/TextView07"
android:paddingTop="20dp" >
</RelativeLayout>
Nu in mijn echte klas wil ik niet de advertentie die ik heb opgenomen:
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "ca-app-pub-xxxxxx/yyyyyy";
/**
* Simply loads the xml about page layout to display the text.
*/
public void onCreate(Bundle start) {
super.onCreate(start);
setContentView(R.layout.about);
final TelephonyManager tm = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
//String deviceid = tm.getDeviceId();
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.test);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.build();
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice(deviceid).build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
Nu idealiter u een slimme banner wilt gebruiken om uw lay-out voor alle apparaten te optimaliseren, zoals ik hierboven heb, is dit nu standaard.
Als dit geen help is om te gaan naar hier
Antwoord 4
u moet de xmlns:ads in uw bovenliggende lay-out vervangen
xmlns:ads="http://schemas.android.com/tools"
**by this**
xmlns:ads="http://schemas.android.com/apk/res-auto"
Antwoord 5
Ik heb geprobeerd de naamruimtedeclaratie te wijzigen, maar het werkt gewoon niet. Ik heb dit opgelost door een hoogtewaarde op te geven voor de adView in plaats van wrap_content
. Het werkt voor mij
<com.google.android.gms.ads.AdView
android:id="@+id/adView2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center|bottom"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
Antwoord 6
Voor iedereen die geïnteresseerd is, is hier een Kotlin-versie.
<RelativeLayout
android:id="@+id/adContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
</RelativeLayout>
En in de code erachter
//create a new AdView
val adView = AdView(this)
adView.adSize = AdSize.SMART_BANNER
adView.adUnitId = "your adUnitID goes here or you can use a function"
adView.visibility = View.GONE
//add it to the layout
val layout = adContainer
layout.addView(adView)
//profit
MobileAds.initialize(this) {}
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
adView.adListener = object : AdListener(){
override fun onAdLoaded() {
adView.visibility = View.VISIBLE
super.onAdLoaded()
}
}