我的问题是 AdMob 广告没有显示在我在物理设备上的测试应用中。我几乎在这里浏览了所有类似的问题,没有运气。所以我的 xml 中有经典的 AdView 声明:
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="my_actual_publisher_id"
ads:adSize="BANNER"/>
我的 list 包含所需的权限、广告 Activity 和 Google Play 服务:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
我确保我的广告有足够的空间:我将它放在一个 TableRow 中,并且下一个 TableRow 有足够的空间来显示。为我的 AdView “预留”的空间与我的预期一致。
在相应的 onCreate() fragment 中,我请求一个新广告(我已经调试了那段代码,它被正确执行):
AdView adView = (AdView) result.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("my_real_device_id").build();
adView.loadAd(adRequest);
我的 AdMob 信息中心没有显示任何请求...我做错了什么?谢谢
编辑:一个奇怪的事实也许值得一提:我试图在 TableLayout 中向上移动包含我的广告的 TableRow,以防出现渲染空间或类似问题。现在,当我尝试以编程方式获取曾经在我的广告之前而现在是之后 的 View 时,我现在遇到了类转换异常:
按钮 clearButton = (Button) view.findViewById(R.id.clearButton);
在我的 activity.onCreate() 中,我正在执行上面的代码来执行一些初始化,这段代码现在失败并出现异常:
java.lang.ClassCastException: android.widget.TableRow 无法转换为 android.widget.Button
就好像我的R类没有被重新编译,它的指针指向哪里!!!???
最佳答案
我过去常常在我的应用程序中添加这个简单的代码。希望它也能帮到你... :)
private static final String AD_UNIT_ID = "ca-app-pub-7742695752659854/6319679360"; // admob id
// ******************************** For Admob
private void createBanner() {
// Create an ad.
adView = new AdView(this);
/*
* Ad unit name : MobilePASystemBanner in the AdMob Website. App Name :
* MobilePASystem .
*/
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
/*
* Add the AdView to the view hierarchy. The view will have no size
* until the ad is loaded.
*/
// a linear layout at the bottom to display an add (make it in your xml also)
mlnr_displayBanner.addView(adView);
/*
* Create an ad request.
*/
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
/*
* Start loading the ad in the background.
*/
try {
adView.loadAd(adRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
在要显示添加的地方调用此方法。
将此添加到您的 list 中:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
不要忘记添加库。
************** 检查您是否从这里获取您的添加单元 ID *******************
关于安卓adMob : ads not showing (tried everything),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474477/