2014-09-28 177 views
3

這是我的第一個Android應用程序,我正在嘗試爲我的應用程序使用admob廣告,但儘管我已經完成了所有操作,但它仍無法正常工作。Admob測試廣告沒有顯示

這是關係到廣告

 protected void onCreate(Bundle savedInstanceState) { 
........ 

    LinearLayout rootLayout = (LinearLayout) findViewById(R.id.ForViewAd); 

    AdView adView = new AdView(this); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    //i've gotten the test ad id by first setting this as 'abc' 
    //and then reading off the logcat 
    adView.setAdUnitId("XXXXXXXXX"); 

    rootLayout.addView(adView, 0); 

    AdRequest adRequest = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .addTestDevice("XXXXXXXXXX") 
     .build(); 
    adView.loadAd(adRequest); 

下在我的活動代碼是我的XML代碼

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    ....... 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:orientation="horizontal" > 


    <com.google.ads.AdView 
       android:id="@+id/ForViewAd" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 
    </LinearLayout> 
    </RelativeLayout> 

在對模擬器中運行的應用程序是既陌生又令人困惑的logcat的讀數

 09-28 15:28:21.076: E/Trace(968): error opening trace file: No such file or directory (2) 
     09-28 15:28:21.120: W/GooglePlayServicesUtil(968): Google Play services is missing. 
     09-28 15:28:21.140: I/Ads(968): Starting ad request. 
     09-28 15:28:21.140: I/Ads(968): 
     Use AdRequest.Builder.addTestDevice ("XXXXXXXXX") to get test ads on this device. 
     09-28 15:28:21.200: D/dalvikvm(968): DexOpt: --- BEGIN 'ads326198251.jar' (bootstrap=0) --- 
     09-28 15:28:21.216: D/dalvikvm(968): GC_CONCURRENT freed 155K, 3% free 10994K/11271K, paused 19ms+11ms, total 36ms 
     09-28 15:28:21.324: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x519 dp. 
     09-28 15:28:21.340: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x499 dp. 
     09-28 15:28:21.356: D/libEGL(968): loaded /system/lib/egl/libEGL_emulation.so 
     09-28 15:28:21.372: D/(968): HostConnection::get() New Host Connection established 0xb975ff68, tid 968 
     09-28 15:28:21.380: D/libEGL(968): loaded /system/lib/egl/libGLESv1_CM_emulation.so 
     09-28 15:28:21.380: D/libEGL(968): loaded /system/lib/egl/libGLESv2_emulation.so 
     09-28 15:28:21.432: W/EGL_emulation(968): eglSurfaceAttrib not implemented 
     09-28 15:28:21.448: D/OpenGLRenderer(968): Enabling debug mode 0 
     09-28 15:28:21.476: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb95468e0): name, size, mSize = 1, 1048576, 1048576 
     09-28 15:28:21.532: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb953e248): name, size, mSize = 2, 9216, 1057792 
     09-28 15:28:21.572: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb954fcb8): name, size, mSize = 4, 36864, 1094656 
     09-28 15:28:21.576: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb976f528): name, size, mSize = 5, 16384, 1111040 
     09-28 15:28:21.576: D/dalvikvm(968): DexOpt: --- END 'ads326198251.jar' (success) --- 
     09-28 15:28:21.576: D/dalvikvm(968): DEX prep '/data/data/com.example.testapp/cache/ads326198251.jar': unzip in 0ms, rewrite 375ms 
     09-28 15:28:21.704: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb954edf8): name, size, mSize = 8, 16384, 1127424 
     09-28 15:28:21.704: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb955e380): name, size, mSize = 9, 4096, 1131520 
     09-28 15:28:21.776: D/dalvikvm(968): GC_CONCURRENT freed 214K, 4% free 11181K/11527K, paused 14ms+1ms, total 22ms 
     09-28 15:28:21.780: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x519 dp. 
     09-28 15:28:21.788: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x499 dp. 
     09-28 15:28:22.040: W/Ads(968): Invalid unknown request error: [Cannot determine request type. Is your ad unit id correct?] 
     09-28 15:28:22.048: W/Ads(968): There was a problem getting an ad response. ErrorCode: 1 
     09-28 15:28:22.060: W/Ads(968): Failed to load ad: 1 
     09-28 15:38:01.600: D/dalvikvm(968): GC_CONCURRENT freed 398K, 5% free 11181K/11719K, paused 10ms+1ms, total 18ms 
     09-28 15:38:01.604: D/webviewglue(968): nativeDestroy view: 0xb97947e8 
     09-28 15:38:01.608: D/webviewglue(968): nativeDestroy view: 0xb97da500 

正如您所看到的,logca中至少顯示3個錯誤噸。首先,谷歌播放服務已被導入到項目中。我可以在android私有庫文件夾中看到它(但不在libs文件夾中)。

還有一個錯誤,說沒有足夠的空間,但我已經把relativelayout的左右填充設置爲0sp。所以應該有足夠的空間寬度?

最後,錯誤代碼1或「無效的未知請求錯誤」?我不明白那一個。

除了logcat以及廣告未顯示在活動任何位置的事實之外,該應用程序按正常情況運行時沒有任何問題。

有人可以告訴我我做錯了什麼嗎?

下面是什麼建議

09-29 17:31:47.290: D/jdwp(1025): Got wake-up signal, bailing out of select 
    09-29 17:31:47.290: D/dalvikvm(1025): Debugger has detached; object registry had 1 entries 
    09-29 17:31:47.302: W/GooglePlayServicesUtil(968): Google Play services is missing. 
    09-29 17:31:47.302: I/AndroidRuntime(1025): NOTE: attach of thread 'Binder_3' failed 
    09-29 17:31:47.318: I/Ads(968): Starting ad request. 
    09-29 17:31:47.318: I/Ads(968): Use AdRequest.Builder.addTestDevice("XXXXXX") to get test ads on this device. 
    09-29 17:31:47.418: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x519 dp. 
    09-29 17:31:47.422: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x499 dp. 
    09-29 17:31:47.446: W/EGL_emulation(968): eglSurfaceAttrib not implemented 
    09-29 17:31:47.454: D/OpenGLRenderer(968): TextureCache::get: create texture(0xb820e8e0): name, size, mSize = 38, 1048576, 1131520 
    09-29 17:31:47.526: I/ActivityManager(324): Displayed com.example.testapp/com.example.testapp.MainActivity: +249ms 
    09-29 17:31:47.530: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x519 dp. 
    09-29 17:31:47.530: W/Ads(968): Not enough space to show ad. Needs 360x50 dp, but only has 340x499 dp. 
    09-29 17:31:47.606: W/Ads(968): Invalid unknown request error: [Cannot determine request type. Is your ad unit id correct?] 
    09-29 17:31:47.610: W/Ads(968): There was a problem getting an ad response. ErrorCode: 1 
    09-29 17:31:47.610: W/Ads(968): Failed to load ad: 1 
    09-29 17:31:47.622: D/dalvikvm(968): GC_CONCURRENT freed 256K, 4% free 11580K/11975K, paused 1ms+1ms, total 15ms 
    09-29 17:31:47.622: D/webviewglue(968): nativeDestroy view: 0xb85bfb60 
+0

解決方案提供瞭解決方案嗎? – 2014-09-29 13:23:28

+0

不是。這些錯誤似乎從以前就減少了,但廣告仍然沒有顯示。 – user65163 2014-09-29 17:47:07

+0

這個例外與顯示廣告的空間有關。選中「沒有足夠空間顯示廣告,需要360x50 dp,但只有340x519 dp」。 – 2014-09-29 19:59:04

回答

4

smart_banner存在問題 //沒有足夠的空間顯示廣告。需要360x50的DP

橫幅廣告工作正常,

我解決這個像... 從佈局中移除廣告並放入根部佈局,刪除根部佈局的邊距

<RelativeLayout> 
    <LinearLayout> 
    </LinearLayout> 
    <layout> 
    </layout> 
//here 
<com.google.android.gms.ads.AdView /> 
</RelativeLayout> 
+0

「刪除根部佈局的邊距」適用於我。謝謝 – Ponmalar 2015-07-17 06:29:43

+0

刪除填充和邊距爲我工作,謝謝 – 2015-09-04 10:38:22

0

您還沒有正確添加爲TestDevice ID後logcat中,我發現你的設備ID在你的日誌和更新的代碼。

AdRequest adRequest = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .addTestDevice("5C2521D171291728A381823D5ACC9B8D") 
     .build(); 
    adView.loadAd(adRequest); 

接下來,在你的佈局文件只是下面是不夠的,因爲你的Java代碼設置adUnitId設置,testdeviceid和adSize時。你可以把它放在java代碼或佈局文件中。

<com.google.ads.AdView 
       android:id="@+id/ForViewAd" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 

包括谷歌的更多信息,https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start

+0

剛剛嘗試了您的建議修復。不幸的是,似乎仍然出現類似的錯誤。我已經用你的方法更新了上面的logcat – user65163 2014-09-29 17:30:50

0

添加到@蘭芝斯的回答官方頁面,因爲你通過谷歌遊戲服務使用AdMob的,你應該修改你的佈局如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
....... 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:orientation="horizontal" > 

您可以按照official guide從舊admob遷移到新admob

順便說一句,將來不要在互聯網上發佈正確的adunit id。我已將其屏蔽掉

+0

對不起。我會把這個adunit id掩蓋出來。 – user65163 2014-09-29 17:37:29