这是我的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>
只要广告没有显示,应用程序就可以正常运行。是因为我有机器人提供了AD_UNIT_ID或者其他任何错误,例如将adview设置为wrap_content因为它有nt加载高度将为零。互联网连接是非常非常慢的原因吗?
广告两侧都有空白区域。如何用广告覆盖它
答案 0 :(得分:0)
您必须提供"MY_AD_UNIT_ID"
才能展示广告。检查项目log-cat。有一段时间,它不显示广告,因为广告sdk在日志"NO inventory to show ad"
因此,请将Ad UNIT ID
和log cat
检查以查看问题。
由于
答案 1 :(得分: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;
}
}