-1

以下代码是我为了检查 admob 广告检索而实施的,但我无法获得任何广告,尽管代码甚至没有崩溃。请告知此代码中有什么问题。

public class MainActivity extends Activity {

    public AdView  admobView;
    public View mainView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        admobView = new AdView(this, AdSize.BANNER, "Top secret");
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        admobView.setLayoutParams(lp);
        //mainView.setLayoutParams(lp);

        RelativeLayout layout = new RelativeLayout(this);

        AdRequest adRequest = new AdRequest();

        //tested this
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);               // Emulator

        //tested this
        //adRequest.addTestDevice("20cb18e8ffc74ad2");

        //tested this
        //adRequest.addMediationExtra("3d879f364d5747c5", null);
        admobView.loadAd(adRequest);
        layout.addView(admobView);
        //layout.addView(mainView);

        setContentView(layout);
    }
4

1 回答 1

2

您是否在清单中添加了 admob 的权限和配置更改。因为我已经运行了你的代码,它显示在我的模拟器中。而且为了显示添加您的应用程序的 targetSdkVersion 需要大于 14。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

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

活动和你一样

public AdView  admobView;
    public View mainView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        admobView = new AdView(this, AdSize.BANNER, "a14f39f0b52dce6");
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        admobView.setLayoutParams(lp);
        //mainView.setLayoutParams(lp);

        RelativeLayout layout = new RelativeLayout(this);

        AdRequest adRequest = new AdRequest();

        //tested this
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);               // Emulator

        //tested this
        //adRequest.addTestDevice("20cb18e8ffc74ad2");

        //tested this
        //adRequest.addMediationExtra("3d879f364d5747c5", null);
        admobView.loadAd(adRequest);
        layout.addView(admobView);
        //layout.addView(mainView);

        setContentView(layout);
    }

清单是==

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ee"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
        </activity>
    </application>

</manifest>
于 2012-09-16T14:39:54.800 回答