我无法在 android 应用程序上加载广告,我提供了正确的 id 并且一切都正确。这是代码
public class MainActivity extends Activity {
private static final String MY_BANNER_UNIT_ID = "a15264b25a5a98c";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
AdView adView = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
layout.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
}
XML文件是
<com.example.advertise.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
googleads:adSize="BANNER"
googleads:adUnitId="a15264b25a5a98c"
/>
我得到的错误是
10-21 11:51:15.194: E/AndroidRuntime(8556): FATAL EXCEPTION: main
10-21 11:51:15.194: E/AndroidRuntime(8556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidadvertising/com.example.androidadvertising.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.advertise.AdView
10-21 11:51:15.194: E/AndroidRuntime(8556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-21 11:51:15.194: E/AndroidRuntime(8556): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)