0

我无法在 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)
4

2 回答 2

1

将此行添加到 XML 文件的主布局

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads

<com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/adMob" <--YOUR ID HERE
        ads:loadAdOnCreate="true" >
    </com.google.ads.AdView>

并将此行添加到您的AndroidManifeast.xml

<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation" />

不需要java代码...

于 2013-10-21T06:36:05.917 回答
1

试试这个,使用布局充气器你会得到它

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);
//Try this
LayoutInflater li = LayoutInflater.from(YourActivity.this);
View v = li.inflate(R.layout.youraddmoblayout,null,false);


layout.addView(v);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);

}
}

Youraddmoblayout.xml 像这样

  <com.google.ads.AdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/adMob"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
于 2013-10-21T06:34:55.900 回答