如何让我的广告横幅显示在我的应用上,而无需将整个应用移开?

时间:2014-03-22 20:43:33

标签: java android eclipse admob banner

我正在使用此处的示例代码:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play

我的问题是广告正在显示,但现在我的应用无法运行。我的显示与链接页面中显示的图片完全相同。广告显示,它可以运作,但现在就是它:广告和空白。

我可能做错了什么?

AndroidManifest

<application android:icon="@drawable/ic_launcher"
             android:label="@string/app_name" >
    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity android:name="com.google.android.gms.ads.AdActivity"
         android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

layout - main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adBanner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
</LinearLayout> 

MainActivity.java(我的android包中的主要类)

package com.google.android.gms.ads.banner;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import -omitted-
import android.os.Bundle;
import android.widget.LinearLayout;
import -omitted-

public class MainActivity extends AndroidApplication {
/**
 * A simple {@link Activity} that embeds an AdView.
 */
  /** The view to show the ad. */
  private AdView adView;

  /* Your ad unit id. Replace with your actual ad unit id. */
  private static final String AD_UNIT_ID = "ca-app-pub-1350996374659154/6187192628";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = false;

    initialize(new Beacon(), cfg);

    setContentView(R.layout.main);

    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.MainActivity);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("BA806B06E9FDE5DEDF704F86521731EC")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
}
  @Override
  public void onResume() {
    super.onResume();
    if (adView != null) {
      adView.resume();
    }
  }

  @Override
  public void onPause() {
    if (adView != null) {
      adView.pause();
    }
    super.onPause();
  }

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();

  }
  }

1 个答案:

答案 0 :(得分:1)

您已使用添加了AdView的空白布局调用 setContentView(R.layout.main); 。我希望它能够显示AdView,而这正是它正在做的事情。

您还没有说的是您也在使用libgdx并调用了libgdx方法AndroidApplication #initialize,它还设置了Activity ContentView。你应该打电话给#setCOntentView一次。

如果你想在你的布局中使用libgdx并包含其他元素(例如AdView),那么你需要找到一个与其他人一起玩得更好的libgdx方法。