2014-04-01 19 views
0

這裏是我的MainActivity.java代碼應用程序工作但廣告不顯示?

package com.example.ads; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 

import com.google.android.gms.ads.*; 

public class MainActivity extends Activity { 

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

    // Look up the AdView as a resource and load a request. 
    AdView adView = (AdView)this.findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    adView.loadAd(adRequest); 
} 

@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; 
} 

} 

下面是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:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<com.google.android.gms.ads.AdView android:id="@+id/adView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        ads:adUnitId="MY_AD_UNIT_ID" 
        ads:adSize="BANNER"/> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

</RelativeLayout> 

我的代碼的應用程序工作正常,只是在廣告中不會顯示出來。是因爲我有bot提供了一個AD_UNIT_ID或者是其他錯誤,比如將adview設置爲wrap_content,因爲它的加載高度是零。是非常非常慢的互聯網連接的原因? enter image description here

上有ad.How兩側的白色空間做我的廣告

+0

如果互聯網不工作,那麼這可能是問題所在。 –

+1

adView.setApid(「154050」);這是我的appid用你自己的appid替換它,並希望它的工作:) ..也在你的XML文件中設置你的appid這一行ads:adUnitId =「yourappid」。 –

+0

所以我應該用154050替換MY_AD_UNIT_ID。 – user3404195

回答

0

(見貼出的問題完成下面的討論) 只是複製XML文件,並將其粘貼到您的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:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<com.google.android.gms.ads.AdView android:id="@+id/adView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    ads:adUnitId="MY_AD_UNIT_ID" 
    ads:adSize="BANNER"/> 


<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/hello_world" /> 

</RelativeLayout> 

試試這個,希望現在它的工作:

package com.example.ads; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 

import com.google.android.gms.ads.*; 

public class MainActivity extends Activity { 

private static final int BANNER_AD_WIDTH = 320;  

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

// Look up the AdView as a resource and load a request. 
AdView adView = (AdView)this.findViewById(R.id.adView); 
AdRequest adRequest = new AdRequest.Builder().build(); 
adView.loadAd(adRequest); 

int placementWidth = BANNER_AD_WIDTH; 
adView.setWidth(placementWidth); 
} 

@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; 
} 

} 
+0

它沒有工作。這正是我已經寫過的。 – user3404195

+0

我試過它的廣告仍然是相同的大小。 – user3404195

+0

標記爲答案thanx。 – user3404195

0

你必須要給"MY_AD_UNIT_ID"展示廣告覆蓋。檢查你的項目日誌貓。有些時候,它不顯示廣告,因爲廣告sdk顯示消息日誌"NO inventory to show ad"

因此,把你Ad UNIT ID和檢查log cat看到問題。

感謝

相關問題