如何使用setContentView(new Activity(this))以编程方式创建admob标语 不使用xml?

the*_*osh 3 java android admob android-layout android-studio

这是onCreate(); 我的2d无尽跑步游戏的Game.java活动方法。我是Java和android的新手。我完成了游戏,可以发布到Playstore了。但是我无法在游戏中添加admob横幅。我搜索过的admob教程都以.xml布局结尾,但是我的setContentView(); 执行java类。请帮我解决问题。

public class Game extends Activity {

MediaPlayer bgsound;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //turn title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


    setContentView(new GamePanel(this));



    //sound
    bgsound = MediaPlayer.create(Game.this, R.raw.bgsound);
    bgsound.setLooping(true);
    bgsound.start();

}
Run Code Online (Sandbox Code Playgroud)

pez*_*pez 5

以编程方式创建布局容器:

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

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    // Create a banner ad
    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("myAdUnitId");

    // Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

    // Optionally populate the ad request builder.
    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

    // Add the AdView to the view hierarchy.
    layout.addView(mAdView);

    // Start loading the ad.
    mAdView.loadAd(adRequestBuilder.build());

    setContentView(layout);
}
Run Code Online (Sandbox Code Playgroud)

如果您看到此错误代码W/Ads? Failed to load ad: 0

检查您是否在运行最新版本的Google Play服务build.gradle

dependencies {
    compile 'com.google.android.gms:play-services-ads:7.5.0'
    }
Run Code Online (Sandbox Code Playgroud)

检查您是否具有正确的权限Manifest.xml

<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)

检查您在中的<meta-data>标签是否正确Manifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <!--This meta-data tag is required to use Google Play Services.-->
    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <activity...
Run Code Online (Sandbox Code Playgroud)

检查您的设置是否正确AdActivity

<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:theme="@android:style/Theme.Translucent" />
Run Code Online (Sandbox Code Playgroud)

检查您的广告单元ID是否正确。为了进行测试,您可以在以下位置res/strings.xml或其他位置使用此单位ID :

<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
Run Code Online (Sandbox Code Playgroud)

完整指南在这里